import io.netty.channel.ChannelFactory;
import org.apache.tools.ant.taskdefs.condition.Socket;
import org.springframework.expression.spel.ast.Selection;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
public class NioLXService {
private String ip;
private int port;
private static Selector selector;
private static ServerSocketChannel serverSocketChannel;
public static void main(String[] args) {
NioLXService nioLXService = new NioLXService();
new Thread(()-> {
try {
while (true) {
if(selector.isOpen()){
int readyChannels = selector.select();
if(readyChannels==0)continue;
Set<SelectionKey> selectionKeys = selector.selectedKeys();
if(selectionKeys != null){
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()){
SelectionKey selectionKey = iterator.next();
if(selectionKey.isConnectable()){
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector,SelectionKey.OP_ACCEPT);
System.out.println("连接完成");
}
if(selectionKey.isAcceptable()){
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector,SelectionKey.OP_READ);
System.out.println("注册完成");
}
if(selectionKey.isReadable()){
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int k = socketChannel.read(byteBuffer);
while (k > 0) {
byteBuffer.flip();
byte by[] = new byte[1024];
while (byteBuffer.hasRemaining()) {
System.out.print((char) byteBuffer.get());
}
byteBuffer.clear();
k = socketChannel.read(byteBuffer);
}
socketChannel.register(selector, SelectionKey.OP_WRITE);
System.out.println("正在读取");
}
if(selectionKey.isWritable()){
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
String httpResponse = "HTTP/1.1 200 OK\r\n" +
"Content-Length: 37\r\n" +
"Content-Type:text/html;charset=utf-8\r\n" +
"\r\n" +
"<html><body>你好,世界!</body></html>";
byteBuffer.put(httpResponse.getBytes());
byteBuffer.flip();
while (byteBuffer.hasRemaining()) {
socketChannel.write(byteBuffer);
}
socketChannel.close();
System.out.println("正在写入");
selectionKey.cancel();
}
iterator.remove();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
public NioLXService(){
this("localhost",1234);
}
public NioLXService(String ip,int port){
this.ip = ip;
this.port = port;
try {
selector = Selector.open();
this.serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(ip,port));
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
System.out.println("服务端启动成功,IP:"+ip+"端口为:"+port);
}catch (IOException e){
e.printStackTrace();
}
}
}
java NIO服务端练习,只能输出你好世界。