服务器端: Java 代码: import ; import .InetSocketAddress; import .ServerSocket; import .Socket; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class NIOSServer { private int port = 8888; // 解码 buffer 不败战神: vate Charset cs= ( "gbk" ); /* 接受数据缓冲区*/ private static ByteBuffer sBuffer = ( 1024 ); /* 发送数据缓冲区*/ private static ByteBuffer rBuffer = ( 1024 ); /* 映射客户端 channel */ private Map<String, SocketChannel> clientsMap = new HashMap<String, SocketChannel> (); private static Selector selector; public NIOSServer ( int port ){ = port; try { init (); } catch ( Exception e){ (); }} private void init () throws IOException{ /* * 启动服务器端,配置为非阻塞,绑定端口,注册 accept 事件帝尊: *ACCEPT 事件:当服务端收到客户端连接请求时,触发该事件*/ ServerSocketChannel serverSocketChannel = (); ( false ); ServerSocket serverSocket = (); ( new InetSocketAddress ( port )); selector = (); ( selector, ); ( "server start o
服务器端与多客户端 信息交互 来自淘豆网www.taodocs.com转载请标明出处.