客户端用非阻塞的socketChannel服务器使用serversocket为什么会在服务端接收客户端数据时报Socketclosed?1Socket客户端和ServerSocket服务器--------读写正常2SocketChannel和Server...
客户端用非阻塞的socketChannel 服务器使用serversocket 为什么会在服务端接收客户端数据时报Socket closed?
1 Socket 客户端和ServerSocket 服务器 --------读写正常
2 SocketChannel 和ServerSocketChannel --------读写正常
3 NIO的客户端(SocketChannel且socketChannel.configureBlocking(false);)连接普通的serversocket服务器 会在服务端报Socket closed 这是什么原因呢? 使用的代码都是网上非常常见的NIO后IO的通信实例
java.io.EOFException
at java.io.DataInputStream.readFully(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at ReadHandlerThread.run(Server.java:78)
at java.lang.Thread.run(Unknown Source)
java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.write(Unknown Source)
at WriteHandlerThread.run(Server.java:137)
客户端:
socketChannel = SocketChannel.open();
SocketAddress socketAddress = new InetSocketAddress("localhost", 8000);
socketChannel.connect(socketAddress);
socketChannel.configureBlocking(false);
sendData(“11111111”);
receiveData(socketChannel);
服务端:起两个线程分别处理接收和发送
Socket client = serverSocket.accept();
// 一个客户端连接就开户两个线程处理读写
new Thread(new ReadHandlerThread(client)).start();
new Thread(new WriteHandlerThread(client)).start();
dis = new DataInputStream(client.getInputStream());String reciver = dis.readUTF();
展开