如何在双向套接字连接上序列化类似 telnet 的通信?
我已经实现了在同一台机器上运行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是您从套接字读取的内容超出了您的预期,而不是数据包冲突。难道你上次从套接字读取的内容看起来像这样
,而在下次读取时你会得到“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
and on the next read you get "EIVED"?