通过套接字发送消息之前的延迟 - 这有什么帮助?
我有一个连接第三方软件应用程序的 tcpip 套接字接口。我已经为几个客户站点实现了这个界面,没有出现任何问题。不过,最新的客户……有问题。我们已打开两端应用程序的日志记录,并在 PC 上安装了 Wireshark 来记录原始 tcpip 流量。这样,我们就证明了我的服务器应用程序成功发送了消息,电脑收到了消息,但客户端应用程序看不到它。 (这是一个完全间歇性的问题,这就是为什么排除故障如此痛苦的原因。)
套接字详细信息非常简单:一个套接字处理服务器和 PC 之间的双向通信。这些消息是纯 ascii 文本并且相当短(不是 XML)。服务器通过发送第一条消息来发起通信,然后客户端用多条消息进行响应。应用程序运行时,套接字始终保持打开状态。客户端应用程序的设计使最终用户一次只能处理一个案例,从而防止发生消息冲突。他们设置了某种轮询,他们的应用程序“休眠”,直到看到来自服务器的启动消息。
第三方供应商建议我在向他们发送启动消息之前添加几秒钟的延迟。我不明白这有什么帮助。如果客户端正在“睡眠”,只是轮询等待消息的套接字,那么在第一条消息之前添加延迟有何帮助?这不像我们发送了两条消息而第二条消息丢失了。它正在丢失第一条消息。所以我不明白我们现在发送该消息还是两秒后发送该消息有什么关系。
我问过他们,他们没有给我详细信息。他们可能不想向我透露他们编码中的一些专有细节,这是公平的。所以我在这里问是因为我一直在学习有关套接字编程的新知识。也许你们可以阐明轮询 tcpip 套接字如何受到消息计时的影响?
I have a tcpip socket interface to a third party software app. I've implemented this interface for several customer sites with no problem. The latest customer, though... problems. We've turned on logging in the apps on either end, and also installed Wireshark on the PC to log raw tcpip traffic. With that, we've proved that my server app successfully sends the message out, the pc receives the message, but the client app doesn't see it. (This is a totally intermittent problem, which is why it's such a pain to troubleshoot.)
The socket details are as simple as they come: one socket handling two way communications between the server and the pc. The messages are plain ascii text and fairly short (not XML). The server initiates communications by sending the first message, and then the client responds with several messages. The socket is kept open at all times while the apps are running. The client app is designed so that the end user can only process one case at a time, which prevents message collisions from happening. They have some sort of polling set up, their app "hibernates" until it sees the initiating message from the server.
The third party vendor has advised me to add a few second delay before I send them the initiating message. I can't see how that helps. If the client is "sleeping", just polling the socket waiting for a message, how does adding a delay before the first message help? It's not like we send two messages and the second one gets lost. It's losing the first message. So I don't see how it matters if we send that message now or two seconds from now.
I've asked them and they haven't given me details. It could be some proprietary details in their coding that they don't want to disclose to me, and that's fair. So I'm asking here because I'm always learning new things about socket programming. Maybe you guys can shed some light on how polling a tcpip socket can be affected by message timing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于它是其他人的客户端,并且他们不会告诉您它在做什么(除了说“插入延迟”),答案可能是他们的客户端正在读取并丢弃消息,因为它尚未处于处理它的状态。延迟将使客户端有时间进入可以正确响应消息的状态。
换句话说,客户端存在竞争条件。发生这种情况的一种简单方法是,他们有一个线程用于读取消息,另一个线程用于处理消息。
如果没有在客户端上运行 strace(1) 来查看它正在执行什么系统调用,则很难判断客户端实际上在做什么。
Since its someone else's client and they won't tell you what its doing (other than saying 'insert a delay'), the answer is probably that their client is reading and discarding the message because its not yet in a state to deal with it. The delay will allow the client time to get into a state where it can respond to the message properly.
In other words, the client has a race condition. One easy way this can happen is if they have one thread for reading messages and another for dealing with them.
Short of running strace(1) on the client to see what system calls it is making, its tough to tell what the client is actually doing.