0

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,

4

2 回答 2

1

首先,感谢大家为解决此问题的发起和投入。

是的,这个问题是由于对客户端和服务器之间的通信的误解而出现的。服务器以多个数据包的形式发送响应消息 - 它按照标头、消息和尾包的顺序发送数据。所以我的客户的 - Receive 方法假定它在第一次收到标头并尝试反序列化为对象并失败后立即收到完整消息

当我作为故障排除的一部分给出睡眠声明时,它正在工作,因为它在这段时间内收到了完整的消息。

现在,作为解决方案的一部分,我更新了服务器应用程序以缓冲完整消息并一次写入网络。

于 2013-07-11T09:06:09.930 回答
0

您应该检查以下内容:

  • 服务器是否响应您的所有请求?
  • 你的请求是同步的吗?
  • 请求的超时时间是否太小?

无论如何,你需要醒来,不要再睡了。

于 2013-07-10T15:58:37.373 回答