开发者

TCPClient not reading the incoming data

I am working on a C# Application which connects to the host over TCP/IP to send and then receive the response using TCPClient Stream.

The Problem is I can send data using the Stream.Write(...) but when I try to get the response against my sent data by calling Stream.Read(...), it hangs and never returns.

I've checked the Network sniffing tool like Wire-Shark and can see that the data from the host is being received by my network interface. Why this data is not getting read by my TCPClient?

The Hex dump of the received data shown by WireShark starts with 00, does it mean a Null character, creating problem for the TCPClient to read?

00-1d-4f-fb-43-4b-00-1d-7e-3a-9a-20-08-00-45-00-00-34

Here's the code for writing ...

myTcpClient = new TcpClient();
myTcpClient.Connect("192.168.0.19开发者_开发知识库4", 1958);
Stream myTCPStream = myTcpClient.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] msgBytes = asen.GetBytes("Message to be sent.");
myTCPStream.Write(msgBytes, 0, msgBytes.Length);

and that is the code for reading ...

byte[] bytesReceived = new byte[100];
int nBytesRead = myTCPStream.Read(bytesReceived, 0, bytesReceived.Length);
myTcpClient.Close();

Thanks for help.


This could be a timing issue, but it is hard to say without seeing your code (are you running the read code directly after you send the packet? is it on the same thread? )

Another thought: Try setting the NoDelay property to true. This tells the TCP stack to pass data to your app immediately rather than waiting for a full buffer (disabling the Nagle algorithm). This is useful for reducing latency when sending & receiving small packets of data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜