首页  

java NIO http server     所属分类 nio 浏览量 1018
nio http server 
短连接 

key.isAcceptable()
key.isReadable()
key.isWritable()

key.cancel();
// 写完就关闭  
socketChannel.close();


浏览器访问测试

doWrite done,time=0,java.nio.channels.SocketChannel[connected local=/127.0.0.1:8000 remote=/127.0.0.1:59105]
doWrite done,time=1,java.nio.channels.SocketChannel[connected local=/127.0.0.1:8000 remote=/127.0.0.1:59106]

remote=/127.0.0.1:59105
remote=/127.0.0.1:59106
浏览器打开的端口一直在变


完整代码 https://gitee.com/dyyx/hellocode/blob/master/src/nio/SimpleNioHttpServer.java import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.time.LocalDateTime; import java.util.Iterator; import java.util.Set; public class SimpleNioHttpServer { private static final int PORT = 8000; public static void main(String[] args) throws IOException { int port = getPort(); Selector selector = Selector.open(); ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(port), 1024); serverSocketChannel.configureBlocking(false); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); System.out.println("SimpleNioHttpServer start,port="+port+","+serverSocketChannel); while (true) { selector.selectNow(); Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> it = selectionKeys.iterator(); while (it.hasNext()) { SelectionKey selectionKey = it.next(); it.remove(); handleInput(selector, selectionKey); } } } private static void handleInput(Selector selector, SelectionKey key) throws IOException { // 新连接 if (key.isAcceptable()) { SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept(); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); System.out.println("Accept done,socketChannel="+socketChannel); } if (key.isReadable()) { SocketChannel socketChannel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(4); String requestData = ""; while (socketChannel.read(buffer) > 0) { buffer.flip(); CharBuffer cb = Charset.forName("UTF-8").decode(buffer); requestData+=cb.toString(); buffer.clear(); } System.out.println("requestData="+requestData+"\n"+socketChannel); socketChannel.register(selector, SelectionKey.OP_WRITE); } if (key.isWritable()) { SocketChannel socketChannel = (SocketChannel) key.channel(); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("HTTP/1.1 200 OK\n"); stringBuffer.append("Content-Type: text/html; charset=UTF-8\n"); stringBuffer.append("\n"); stringBuffer.append("hello , SimpleNioHttpServer ,"+LocalDateTime.now()); doWrite(socketChannel, stringBuffer.toString()); key.cancel(); // 写完就关闭 ,短连接 socketChannel.close(); } } private static void doWrite(SocketChannel channel, String response) throws IOException { System.out.println("doWrite start,"+channel); long start = System.currentTimeMillis(); if (response != null && response.trim().length() > 0) { byte[] bytes = response.getBytes(); ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length); writeBuffer.put(bytes); writeBuffer.flip(); channel.write(writeBuffer); } long end = System.currentTimeMillis(); long time = end - start; System.out.println("doWrite done,time="+time+","+channel); } private static final int getPort() { int port = PORT; try { port = Integer.parseInt(System.getProperty("port")); }catch(Throwable e) { // } if(port<=0) { port = PORT; } return port; } }
BIO SimpleHttpServer 长连接 ,仅支持 GET https://gitee.com/dyyx/hellocode/blob/master/src/SimpleHttpServer2.java

上一篇     下一篇
自定义springboot starter

springboot禁用特定的自动配置类

go defer 延迟函数

http request to javabean

数据对象转换Object mapping

mysql时间函数