I
I
Ivan Gromov2015-11-06 18:58:51
linux
Ivan Gromov, 2015-11-06 18:58:51

How is recv received?

There are 2 applications: server and client. The client reads the data from the file, prepends its header, and sends the packet. The server does not know in advance how much data will be received (it knows that it is definitely not more than 5000), so it makes recv with a buffer value of 5000. But it turns out that the client sends 5 times 1000 bytes each, and the server reads 5000 bytes at a time, thereby gluing the sent client data, which is not necessary.
So the question is: how to make it so that I receive exactly as much data as the client sent, without any gluing.
I am writing in C on linux, the recv / send functions used, I don’t see the point in giving the code, because there are 10 lines ...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jcmvbkbc, 2015-11-06
@jcmvbkbc

Using TCP/IP - in any way. Specify the length of the data in your header on the server, and on the client get the header and read as much data as is written there.

O
Oleg Tsilyurik, 2015-11-06
@Olej

So the question is: how to make it so that I receive exactly as much data as the client sent, without any gluing.

If it is TCP (it is necessary to specify it!), then there is no concept of "packet", and if necessary, then you must organize the message yourself, it must be self-defined:
- either contain the length field first, and then the message itself (like POST)
- or end with a predefined delimiter (usually "\n\n")
See Network Programming in Linux for details

A
abcd0x00, 2015-11-07
@abcd0x00

In general, there are means for checking the socket for activity. You can set a time threshold to distinguish parts of one record from different records. But theoretically it is unreliable.
So use TLV
https://en.wikipedia.org/wiki/Type-length-value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question