简体   繁体   English

TCP消息到达同一套接字

[英]TCP messages arrival on the same socket

I've got a 2 services that communicate using a TCP socket (the one that initiates the connection is a C++ Windows service and the receiver is a C# TCP stream) and (let's say that they might not use the same TCP connection all the time). 我有2个服务使用TCP套接字进行通信(启动连接的服务是C ++ Windows服务,接收器是C#TCP流),并且(假设它们可能一直不使用相同的TCP连接) )。 Some of the time I'm getting half messages (where the number of bytes is miscalculated somehow) on a great network load. 有时候,在网络负载很大的情况下,我会收到一半的消息(字节数以某种方式计算不正确)。

I have several questions in order to resolve the issue: 为了解决这个问题,我有几个问题:

  • Can I be sure that messages (not packets...) must follow one another, for example, if I send message 1 (that was received as half), then message 2, can I be sure that the next half of message 1 won't be after message 2? 我可以确定消息(不是数据包...)必须彼此跟随吗,例如,如果我发送消息1(被接收为一半),然后发送消息2,可以确定消息1的下半部分赢了不是在消息2之后吗? Please have separate answer for the same TCP connection between all messages and not having the same TCP connection between them. 对于所有消息之间的相同TCP连接,并且它们之间没有相同的TCP连接,请分别回答。

  • Is there any difference whether the sender and the receiver are on the same station? 发送方和接收方是否在同一站上有什么区别?

TCP is a stream protocol that delivers a stream of bytes. TCP是提供字节流的流协议。 It does not know (or care) about your message boundaries. 它不知道(或关心)您的消息边界。

To send the data, TCP breaks the stream up into arbitrarily sized packets. 为了发送数据,TCP将流分解为任意大小的数据包。 [it's not really arbitrary and it is really complicated.] and send these reliably to the other end. [这不是很随意,而且确实很复杂。]并将其可靠地发送到另一端。

When you read data from a TCP socket, you get whatever data 1) has arrived, and 2) will fit in the buffer you have provided. 当您从TCP套接字读取数据时,您将得到1)到达的任何数据,而2)将适合您提供的缓冲区。

On the receive side you need to use code to reassemble complete messages from the TCP stream. 在接收端,您需要使用代码来重组来自TCP流的完整消息。 You may have to write this yourself or you may find that it is already written for you by whatever library (if any) you are using to interact with the socket. 您可能必须自己编写此代码,或者可能发现您用于与套接字进行交互的任何库(如果有)已经为您编写了该代码。

TCP is a streaming protocol, it doesn't have "messages" or "packets" or other boundaries. TCP是一种协议,它没有“消息”或“数据包”或其他边界。 Sometimes when you receive data you might not get all that was sent, other times you could get more than one "message" in the received stream. 有时,当您接收到数据时,可能无法获得所有已发送的数据,而有时,您可能在接收到的流中获得多个“消息”。

You have to model your protocol to handle these things, for example by including special message terminators or including a fixed-sized header including the data size. 您必须对协议进行建模以处理这些问题,例如,通过包括特殊的消息终止符或包括大小固定的标头(包括数据大小)。

If you don't get all of a message at once, then you have to receive again, maybe even multiple times, to get all the data that was sent. 如果您一次没有收到所有消息,则必须再次接收(甚至多次)以获取所有已发送的数据。

And to answer your question, you can be sure that the stream is received in the order it was sent, or the TCP layer will give you an error. 为了回答您的问题,您可以确保按发送顺序接收到流,否则TCP层将给您一个错误。 If byte A was sent before byte B then it's guaranteed that they will arrive in that order. 如果在字节B之前发送了字节A,则可以保证它们将按该顺序到达。 There is also no theoretical difference if the sender and receiver is on the same system, or on different continents. 如果发送方和接收方在同一系统上或在不同的大陆上,则理论上也没有差异。

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

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