1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| import java.io.Closeable; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.util.HashMap; import java.util.Map;
public class Server { private static final String LOCALHOST = "localhost"; private static final int DEFAULT_PORT = 8888;
private AsynchronousServerSocketChannel serverChannel;
private void close(Closeable closeable){ try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } }
public void start(){ try { serverChannel = AsynchronousServerSocketChannel.open(); serverChannel.bind(new InetSocketAddress(LOCALHOST,DEFAULT_PORT)); System.out.println("服务器启动成功,监听端口号:" + DEFAULT_PORT);
while (true){
serverChannel.accept(null,new AcceptHandler());
System.in.read(); }
} catch (IOException e) { e.printStackTrace(); }finally { close(serverChannel); } }
private class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel,Object> {
@Override public void completed(AsynchronousSocketChannel result, Object attachment) { if(serverChannel.isOpen()){ serverChannel.accept(null,this); }
AsynchronousSocketChannel clientChannel = result; if(clientChannel != null && clientChannel.isOpen()){ ClientHandler handler = new ClientHandler(clientChannel); ByteBuffer buffer = ByteBuffer.allocate(1024);
Map<String,Object> attachmentInfo = new HashMap<>(); attachmentInfo.put("type","read"); attachmentInfo.put("buffer",buffer);
clientChannel.read(buffer,attachmentInfo,handler); }
}
@Override public void failed(Throwable exc, Object attachment) { } }
private class ClientHandler implements CompletionHandler<Integer,Map<String,Object>> { private AsynchronousSocketChannel clientChannel;
public ClientHandler(AsynchronousSocketChannel clientChannel) { this.clientChannel = clientChannel; }
@Override public void completed(Integer result, Map<String, Object> attachment) { String type = (String) attachment.get("type"); ByteBuffer buffer = (ByteBuffer) attachment.get("buffer"); if("read".equals(type)){ buffer.flip(); attachment.put("type","write"); clientChannel.write(buffer,attachment,this); }else if ("write".equals(type)){ ByteBuffer byteBuffer = ByteBuffer.allocate(1024); attachment.put("type","read"); attachment.put("buffer",byteBuffer); clientChannel.read(byteBuffer,attachment,this); } }
@Override public void failed(Throwable exc, Map<String, Object> attachment) {
} }
public static void main(String[] args) { Server server = new Server(); server.start(); } }
|