ServerSocketChannel服务器tcp服务器
NIO服务器,代码如下:
package com.nio; import java.io.IOException; import .InetSocketAddress; import .ServerSocket; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Set; public class NIOServer { private Selector selector; private static final int BLOCK = 4096; private final ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK); private final ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK); public NIOServer(int port) { try { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); ServerSocket serverSocket = serverSocketChannel.socket(); serverSocket.bind(new InetSocketAddress(port)); selector = Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); } catch (IOException e) { e.printStackTrace(); } } public void listen() throws IOException { while (true) { selector.select(); Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey selectionKey = iterator.next(); iterator.remove(); handleKey(selectionKey); } } } private StringBuilder recv = new StringBuilder(); private void handleKey(SelectionKey selectionKey) throws IOException { ServerSocketChannel server = null; SocketChannel client = null; String receiveText; String sendText; StringBuilder send = new StringBuilder(); int count = 0; if (selectionKey.isAcceptable()) { server = (ServerSocketChannel) selectionKey.channel(); client = server.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_READ); } else if (selectionKey.isReadable()) { recv.setLength(0); client = (SocketChannel) selectionKey.channel(); System.out.print("client:"); while (true) { receivebuffer.clear(); count = client.read(receivebuffer); if (count == -1) { break; } receiveText = new String(receivebuffer.array(), 0, count); recv.append(receiveText); if ("\r\n".equals(receiveText)) { break; } System.out.print(receiveText); } System.out.println(); client.register(selector, SelectionKey.OP_WRITE); } else if (selectionKey.isWritable()) { sendbuffer.clear(); client = (SocketChannel) selectionKey.channel(); sendText = "server:"; send.append(sendText); send.append(recv); sendbuffer.put(send.toString().getBytes()); sendbuffer.flip(); client.write(sendbuffer); client.register(selector, SelectionKey.OP_READ); } } public static void main(String[] args) throws IOException { int port = 8888; NIOServer server = new NIOServer(port); server.listen(); } }
在客户端用telnet或者nc来连接
telnet 127.0.0.1 8888
Net 4.5 WebSocket 在 Windows 7, Windows 8 and Server 2012上的比较WebSocket4Net
Net 4.5 WebSocket 在 Windows 7, Windows 8 and Server 2012上的比较以及问题WebSocket4Net
network error:software caused connection abort解决办法network error software caused connection abort
Windows Socket 编程:TCP服务器端windows socket编程
SocketRocket:高效的iOS与macOS实时通信WebSocket库
解决mongooseserverselectionerror: connect econnrefused ::1:27017错误的方法
Docker的三种网络代理配置:dockerd pull镜像代理;容器docker run网络代理;docker build代理--build-argdocker pull 代理
rocketNameServer配置 rocket connector system
Linux中搭建常用服务器(搭建telnet服务器\搭建web服务器\搭建DNS服务器\建DHCP服务器\建FTP服务器)linux安装telnet服务器