0

Given a successful Socket connection, my Java server does this:

out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
out.write("HELLO");
out.newLine();
out.flush();

And this is what my C# client does:

TcpClient tcpClient         = new TcpClient("localhost", port);
tcpClient.ReceiveTimeout    = 10000;
NetworkStream networkStream = tcpClient.GetStream();
BinaryReader reader = new BinaryReader(networkStream);
reader.ReadString(); // <--- Hangs
tcpClient.Close();

It will hang on ReadString() and time out eventually. If you change it to

reader.Read(); // <--- Returns 72

Returns 72 which I assume corresponds to the H. So apparently the Java server is indeed sending the message - why is ReadString() hanging?

Voldemort
  • 17,552
  • 47
  • 139
  • 262

1 Answers1

2

You have a mismatch between the format that you write and then subsequently read. The docs for BinaryReader.ReadString say:

Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.

That is very different to what you are writing.

My guess is that ReadString is getting a nonsensically large "length", and is trying to read that many bytes. Of course, they won't be sent.

Stephen C
  • 669,072
  • 92
  • 771
  • 1,162