1

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.194", 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.

thriller
  • 51
  • 1
  • 5
  • My magic crytal ball still needs to be repaired, so i fear you will have to show us the code in the mean time ... – DarkSquirrel42 May 30 '11 at 10:20
  • I am also facing similar issue. Tried setting NoDelay to true but no luck. Wireshark clearly shows that server has sent this data but network stream is unable to read it. HTTP/1.1 200 Connection established\r\n – amarnath chatterjee Jul 21 '21 at 18:49

1 Answers1

2

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.

rupello
  • 8,361
  • 2
  • 37
  • 34
  • yes, I am reading right after sending in the same thread. The data which is caught by wireshark is of 54 bytes, starting with 0x00. Is it some kind of Ack ??? – thriller Jun 01 '11 at 06:30
  • The data will be whatever is sent back from your server application on receiving a new connection - it looks like a binary protocol of some kind. It may help to post your Wireshark log – rupello Jun 01 '11 at 08:07