简体   繁体   English

TCP / IP框架,散布的消息

[英]TCP/IP framing, interspersed messages

I'm just diving into some TCP/IP sockets programming in .NET, and I'm beginning to understand how to use message framing to distinguish "messages" in the TCP/IP data stream, for example using message start and end delimiters. 我只是深入研究.NET中的一些TCP / IP套接字编程,并且我开始了解如何使用消息框架来区分TCP / IP数据流中的“消息”,例如使用消息开始和结束定界符。

However, I'm not able to wrap my head around what happens if message bytes are sent interspersed with one another. 但是,如果消息字节相互散布,我将无法解决问题。 Consider the following scenario (I'm not even sure if this scenario makes sense, so please let me know if I'm misunderstand something): 考虑以下情形(我什至不确定这种情形是否有意义,因此如果我误会了某些情况,请告诉我):

Rule 1: A web service must send a TCP/IP message to a particular client when it receives a new HTTP request. 规则1:Web服务在收到新的HTTP请求时必须向特定客户端发送TCP / IP消息。

Rule 2: The message that the web service will send via via TCP/IP to the client will be ASCII-formatted, and the message will be "+MESSAGE_TEXT|", with a plus sign as the message start delimiter and a pipe symbol as the message end delimiter. 规则2:Web服务将通过TCP / IP发送到客户端的消息将为ASCII格式,并且消息将为“ + MESSAGE_TEXT |”,其中加号为消息开始定界符,而管道符号为消息结束定界符。

Rule 3: In the client, each byte will be checked as it is received, and when it receives a message 'start' delimiter, it will begin populating a new string with the ASCII-encoded message. 规则3:在客户端中,将在接收到每个字节时对其进行检查,并且在接收到消息“开始”定界符时,它将开始使用ASCII编码的消息填充新字符串。 When a message 'end' delimiter is received, it will know that the message is complete and process it. 收到消息“结束”定界符后,它将知道消息已完成并对其进行处理。

Scenario: The web service receives two HTTP requests simultaneously, which causes two messages to be sent via TCP/IP to the client and when the bytes are received and re-assembled they look like this (ASCII-encoded): "+ME+MESSSSAGE_AGESTART|_START|". 场景:Web服务同时接收两个HTTP请求,这导致两个消息通过TCP / IP发送到客户端,并且在接收并重新组装字节后,它们看起来像这样(ASCII编码):“ + ME + MESSSSAGE_AGESTART | _START |”。

I realize that HTTP requests that are actually "simultaneous" are relatively rare, but since each HTTP Request is essentially in its own thread, this is a possibility. 我意识到实际上“同时”的HTTP请求相对很少,但是由于每个HTTP请求本质上都在其自己的线程中,所以这是有可能的。 Is this a likely scenario? 这可能吗? If so, how do you know which bytes go with which message? 如果是这样,您如何知道哪个字节与哪个消息一起发送? Or am I simply confused about the whole thing? 还是我只是对整个事情感到困惑?

Thanks! 谢谢!

The code that sends data to the client must regulate up the data that is sending on the socket. 将数据发送到客户端的代码必须规范套接字上发送的数据。

If you have multiple threads all using the same connection use a lock on the socket stream and keep the lock till the message has been completely sent, this will keep other threads from causing intermixed messages. 如果所有使用同一连接的多个线程在套接字流上使用锁,并保持该锁直到消息被完全发送,这将防止其他线程导致消息混杂。

The other way to do it is have a dedicated thread reading from a queue of messages and sending them off, then each message gets put on to the queue and they get sent as they get processed. 另一种方法是让专用线程从消息队列中读取并发送出去,然后将每条消息放入队列中,并在处理它们时发送它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM