Determine if data is on the socket and discarded

I am writing an interface under Linux that receives data from a TCP socket. The user provides a buffer in which the received data is stored. If the provided buffer is small, I just want to return an error. The first problem is to determine if the buffer was sufficient. The recv () function returns me the number of bytes actually written to the buffer. If I use the MSG_TRUNC flag specified on the recv () man page, it still returns the same to me. The second problem is dropping data still queued in the socket. Therefore, if I determined that my Buffer provided was small, I just want to erase everything that remains on the socket. Are there any other ways to do this other than closing and opening the socket again or just getting it until there is nothing left? Best wishes <

One suggestion was to simply return until there was nothing left (I will return 0) - but did it end up waiting for the set timeout (in this case, 5 seconds?), Because every time I call recv, is it waiting for data or a timeout?

+1
source share
2 answers

At the TCP level, there is no knowledge of what an application protocol message is. However, there are two most common ways to distinguish between messages in a TCP stream:

  • prefix messages with their size and reading that many bytes, or
  • read until a specific sequence of bytes is found.

TCP :

  • N
  • .

, Tornado IOStream, .

+4

.

, , ? - , , , ? , . , , , . TCP, , "", , .

+1

Source: https://habr.com/ru/post/1525432/


All Articles