Question

I have to send and receive message synchronously through TCP/IP. I have used an example from this link - http://msdn.microsoft.com/en-us/library/kb5kfec7.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 However I have forced to introduce Sleep (mentioned below) between send and receive otherwise I get empty message.

// Send the data through the socket.
int bytesSent = sender.Send(msg);

Thread.sleep(1000) // But, I only wanted to be in sleep until response received. 

// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);

Please suggest how can I only wait until response received. Thanks,

Was it helpful?

Solution

Firstly, thank you all for your initiation and inputs to resolve this issue.

Yes, this issue appeared due to mis-understanding of communication between client and server. Server is sending response message in multiple packets - it send data in the order of header, message and tail packet at the end. so my client's - Receive method assumes that it has received full message as soon as header received first and tried de-serialize to an object and failed

When I had given a sleep statement as part of troubleshooting, it was working because it receives full message within this time.

Now, as part of resolution, I have updated Server application to buffer full message and write to network at a time.

OTHER TIPS

You should check the following:

  • does the server respond to all your requests?
  • is your request synchronous?
  • is the timeout of the request too small?

Anyway, you need to wake up, do not sleep anymore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top