java nio echo server
所属分类 nio
浏览量 875
telnet 连接测试
telnet 127.0.0.1 8000
NioEchoServer start,port=8000
client socketChannel java.nio.channels.SocketChannel[connected local=/127.0.0.1:8000 remote=/127.0.0.1:56357]
receiveData,len=3,data=a
receiveData,len=3,data=b
receiveData,len=3,data=c
收到的数据 带了 回车换行符 ,因此长度多了2
完整代码
https://gitee.com/dyyx/hellocode/blob/master/src/nio/echo/NioEchoServer.java
import java.net.InetSocketAddress;
import java.net.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.nio.charset.Charset;
import java.util.Iterator;
public class NioEchoServer{
private static final Charset UTF8 = Charset.forName("UTF-8");
public static void main(String[] args) throws Exception{
//-Dport=8000
int port = getInt(System.getProperty("port"));
if(port <= 0) {
port=8000;
}
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
ServerSocket serverSocket = serverSocketChannel.socket();
serverSocket.bind(new InetSocketAddress(port));
System.out.println("NioEchoServer start,port="+port);
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while(selector.select()>0){
Iterator< SelectionKey> iterator = selector.selectedKeys().iterator();
while(iterator.hasNext()){
SelectionKey selectionKey = iterator.next();
iterator.remove();
if(selectionKey.isAcceptable()){
ServerSocketChannel server = (ServerSocketChannel)selectionKey.channel();
SocketChannel socketChannel = server.accept();
if(socketChannel==null){
continue;
}
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
System.out.println("client socketChannel "+socketChannel);
}
if(selectionKey.isReadable()){
SocketChannel socketChannel = (SocketChannel)selectionKey.channel();
ByteBuffer readBuffer = ByteBuffer.allocateDirect(1024);
socketChannel.read(readBuffer);
readBuffer.flip();
String receiveData= UTF8.decode(readBuffer).toString();
System.out.println("receiveData,len="+receiveData.length()+",data="+receiveData);
// selectionKey.attach(receiveData);
// 直接写 不用注册写事件
ByteBuffer writeBuffer = ByteBuffer.allocateDirect(1024);
writeBuffer.put(receiveData.getBytes(UTF8));
writeBuffer.flip();
while(writeBuffer.hasRemaining()){
socketChannel.write(writeBuffer);
}
}
if(selectionKey.isWritable()){
SocketChannel socketChannel = (SocketChannel)selectionKey.channel();
String message = (String) selectionKey.attachment();
if(message==null){
continue;
}
selectionKey.attach(null);
ByteBuffer writeBuffer = ByteBuffer.allocateDirect(1024);
writeBuffer.put(message.getBytes());
writeBuffer.flip();
while(writeBuffer.hasRemaining()){
socketChannel.write(writeBuffer);
}
}
}
}
}
private static int getInt(String str) {
try {
return Integer.parseInt(str);
}catch(Throwable e) {
return 0;
}
}
}
上一篇
下一篇
GO字符串拼接
go socket 编程 echo server
java HTTP Server NanoHttpd
GO文件读取
Java锁升级过程
GO内建函数