如何在双向套接字连接上序列化类似 telnet 的通信?

发布于 2024-10-24 00:03:29 字数 444 浏览 0 评论 0原文

我已经实现了在同一台机器上运行的 VB 和 VC++ 程序之间的 TCP/IP 通信。在 VC++ 端,我生成一个侦听连接的线程。在 VB 端,我使用 Winsock API 连接到 C++ 服务器。一切似乎都工作正常,特别是当我手动调试并单步执行通信时。该协议是基于文本的,命令以“\n”终止,并且可能有也可能没有答案。

问题就在这里。我正在尝试实现发送到服务器的命令,这需要一些处理,然后服务器将它们发送回客户端。在简单的条件下,这工作得很好,但在重负载或不同的机器配置下,有时通信会中断。

客户端可能正在发送命令,但服务器可能正在发送先前命令的答案。 VB 客户端以某种方式得到一个支离破碎的答案(例如,不是“DATA RECEIVED”,而是“EIVED”),这破坏了我为跟踪连接而实现的状态机。

所以,是的,客户端和服务器同时通信,这就产生了问题。如何序列化或排队此通信?我知道这类似于以太网上的网络数据包冲突问题。

I have implemented TCP/IP communication between a VB and VC++ programs running on the same machine. On the VC++ side I spawn a thread which listens for connections. On the VB side I use the Winsock API to connect to the C++ server. Everything seems to work fine, especially when I manually debug and step through the communication. The protocol is text based, commands terminate on '\n' and may or may not have an answer.

And here is the problem. I'm trying to implement commands which are sent to the server, this requires some processing and the server sends them back to the client. Under simple conditions this works fine, but under heavy load or different machine configurations sometimes the communication gets interrupted.

The client may be sending a command but the server may be sending the answer of a previous command. The VB client somehow gets a fragmented answer (say, instead of a "DATA RECEIVED" it gets "EIVED") which breaks the state machine I've implemented to track the connection.

So yeah, client and server are talking at the same time and this is making a problem. How do I serialize or queue this communication? I understand this would be similar to the problem of network packet collision on ethernet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2024-10-31 00:03:29

我的猜测是您从套接字读取的内容超出了您的预期,而不是数据包冲突。难道你上次从套接字读取的内容看起来像这样

OTHER STUFF\nDATA REC

,而在下次读取时你会得到“EIVED”?

Rather than packet collision, my guess would be that you're reading more than you expect from the socket. Could it be that your last read from the socket looks something like

OTHER STUFF\nDATA REC

and on the next read you get "EIVED"?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文