2

I am writing a C# app to communicate with a server via a set of 4-10 byte packets. The server is written in Java, and I can put data to it, however, the only way to actually push the packet to the server is by calling the Close() method on the ServerStream, which is not possible for my purposes, as the server emits a packet back to the sender. I have already tried setting

socket.NoDelay = true;
socket.Client.NoDelay = true;

which does not work. Nor does the Flush() method, because it's not yet implemented. The interesting part is, when I have to force-close the application, the server complains that the connection was reset.

The code follows

socket.NoDelay = true;
socket.Client.NoDelay = true;
socket.Connect("127.0.0.1", 6789);
serverStream = socket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes("ping");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)socket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
Console.WriteLine(returndata);

If I were to add the line serverStream.Close(); after where the Flush() method is, the server receives the packet just fine.

What am I doing wrong with my communications layer? Do I just need to use larger packets or is the Nagle algorithm still active?

4

0 回答 0