I try to transfer 1 byte through a socket. The server should return it to the client. But for some reason, both are stuck on reading. Help me figure out where this byte is stuck?
Server code:
public static void main(String[] args) { try ( ServerSocket serverSocket = new ServerSocket(4444); Socket socket = serverSocket.accept(); DataInputStream istream = new DataInputStream(socket.getInputStream()); DataOutputStream ostream = new DataOutputStream(socket.getOutputStream()); ) { socket.setTcpNoDelay(true); int result = istream.readByte(); System.out.println("Server received: " + result); ostream.writeByte(result); ostream.flush(); } catch (Throwable ex) { ex.printStackTrace(); } } Client code:
public static void main(String[] args) { try ( Socket socket = new Socket("localhost", 4444); DataInputStream istream = new DataInputStream(socket.getInputStream()); DataOutputStream ostream = new DataOutputStream(socket.getOutputStream()); ) { socket.setTcpNoDelay(true); ostream.writeByte(10); ostream.flush(); int result = istream.readByte(); System.out.println("Client received: " + result); } catch (Throwable ex) { ex.printStackTrace(); } } Java 1.8.0_111 is used
If you redo the client with sending the second byte, and put sleep on the server at the end, then the first byte comes to the server and returns to the client.
ostream.writeByte(10); ostream.writeByte(11);