I have a program that sends 88 bytes of raw data (not a string) using NetworkStream.Read and NetworkStream.Write.
Byte number 58 happens to have the value 10 (new line). The receiving program instance stream stops reading once this byte is received acting like a ReadLine instead of Read for raw data.
The scenario is consistent that when I changed it to read 32 bytes at a time, it read 32 then 26 (total of 58) stopping at the same byte.
This is when I run the two program instances on different machines connected through the internet using port 21. When I run both instances on the same machine, the whole 88 bytes are received with no problems.
I will use Network Monitor to see where the 30 bytes get lost, but I thought to ask here for suggestions or if someone faced a similar problem.
Edit: Here's the code:
Here is the code. It reads data from one stream and writes them to the other working both ways: `
class ProxyConnection
{
private NetworkStream clientStream;
private NetworkStream serverStream;
public ProxyConnection()
{
.. clientStream = tcpClient.GetStream();
serverStream = tcpServer.GetStream();
..}
private void RouteFromClientToServer()
{
Message message;
while (true)
{
try
{
message = ReadMessageFromClient();
ValidateMessage(message);
SendMessageToServer(message);
}
catch(IOException e)
{
Logger.getInstance().log(e.Message);
break;
}
}
}
private Message ReadMessageFromClient()
{
Message message = new Message();
message.bytes = new byte[MESSAGE_SIZE];
message.bytesCount = clientStream.Read(message.bytes, 0, MESSAGE_SIZE);
Logger.getInstance().log("Size ( " + message.bytesCount + " ) From Client");
return message;
}
private void SendMessageToServer(Message message)
{
serverStream.Write(message.bytes, 0, message.bytesCount);
Logger.getInstance().log("Size ( " + message.bytesCount + " ) To Server");
serverStream.Flush();
}
}