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); 
  • it seems to be played on win7 with Kaspersky Internet Security 17 enabled. It seems that if you write to stream only bytes 10 and 13 (0x0A, 0x0D, line breaks), the entry is simply ignored (there is nothing in wireshark). After the suspension of protection after a while the code works. - zRrr
  • I do not have Kaspersky, JRE1.8.0_111, the code works. - Regent
  • Thank. Unsuccessfully, I chose a byte value. Values ​​10 and 13 Kaspersky eats. I did not know that it takes time to effect the suspension of protection. Without it, everything works as expected. - ilsu87
  • @ ilsu87 then it makes sense to place the solution (disabling Kaspersky) as an answer and accept it. I think it will be useful to others. - Regent

1 answer 1

It's all about the behavior of Kaspersky Internet Security. Tested with installed version of KIS 17.0.0.611 under Windows 10 Home.

Decision:

  1. Suspend protection (antivirus, screens, firewalls).
  2. Wait for some time (20 seconds was enough).