NetworkStream reading zero bytes
-
2012年4月17日 15:29
Hello,
I am working with a 3rd party vendor using a TcpClient to read information. There are a total of 7 requests I can make. For 6 of the requests, I receive a good response using NetworkStream.Read. But, for one of the requests, I always receive zero bytes using the NetworkStream.Read.
The 3rd party vendor ran a trace that showed they were returning a response for the request that I am receiving zero bytes.
Per MSDN:
"If the remote host shuts down the connection, and all available data has been received, the Read method completes immediately and return zero bytes."
I use the exact same method to send and receive all requests as follows:
private string MakeRequest(byte[] data)
{
StringBuilder completeMessage = new StringBuilder();
using (TcpClient client = new TcpClient(ConnectIP, ConnectPort))
{
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);data = new byte[5100];
Int32 bytes = 0;
if (stream.CanRead)
{
do
{
bytes = stream.Read(data, 0, data.Length);
this.WriteConsole("Bytes is " + bytes.ToString());completeMessage.AppendFormat("{0}", Encoding.ASCII.GetString(data, 0, bytes));
}
while (stream.DataAvailable);
}
else
{
if (Environment.UserInteractive)
Console.WriteLine("Cannot read from this network stream");
}
}
}Why would the trace on the 3rd party end show the response as being sent but I'm receiving nothing. Does that mean the server is shutting down the connection before the response is sent? Or, is there another possibility for why I am not receiving a response for that one request even though the 3rd party trace shows that a response is being sent.
We are going to put a trace on our end to see what we receive later on but I thought I'd ask my question now so that I'm prepared.
I'm really appreciate some assistance in resolving this problem.
Thank you very much!!
- 移動 Leo Liu - MSFT 2012年4月18日 8:38 Moved for better support. (From:Visual C# General)
すべての返信
-
2012年4月18日 1:13
A call to Read should only return zero bytes when the peer socket is closed, so I would suggest that you check what is really happening by analyzing what is really going on using Fiddler or similar tools.
Even though this isn't part of your question, I would also double check your receiving code; I'm specifically worried about the way you receive your response. On good connections, you may receive your packets fast enough that you don't notice, but it's entirely possible that you get a fragment of the response the first time you call Read, and that since nothing is on the wire yet (maybe because the next packet was bad and got discarded), stream.DataAvailable returns false. Not only you would end up with a broken response; since TCP is reliable, you will get the missing part the next time around, which would end up desynchronizing your protocol irreversibly.
Remember: TCP is a Stream oriented protocol which isn't naturally broken into records or datagrams; the protocol itself should provide some envelope that allows you to detect (either implicitly or explicitly) where a message starts and ends. Might seem useless on a good and fast network, but glitches happen even there, occasionally.
HTH
--mc- 回答としてマーク Hiline1961 2012年4月18日 15:31
-
2012年4月18日 15:35
Thank you Mario. You are exactly right. The server is closing the connection.
And thank you for the coding suggestions!!!!

