Network programming in Linux. "read ([...])" What happens behind the scenes?

What does ssize_t read(int fd, void * data, size_t count); right ?

In a large number of articles on the Internet it is often written that he is trying to read or the fd descriptor. What does it mean? โ€œHe is tryingโ€: / And how is such a socket arranged? Does the buffer leave a message buffer? Or is a critical operation read? I mean, is there any chance that some packages will be lost if I don't โ€œreadโ€ on time?

EDIT:

I was a little surprised why this is not blocking. Then I wondered why read (...) has other parameters than all the functions that I saw in the code snippets. Finally, I realized that reading (...) is not recv (...). Unlucky that he still worked almost as I expected. And fnuny, as our ascousing tohuhgts paly geams us. (Do not edit) I must admit that the german example has a greater impact on the reader ...

+4
source share
2 answers

Linux will buffer any data coming into the connected TCP socket, by default, up to several megabytes. You do not need to read at the same time that the data arrives.

netstat -tn display netstat -tn Recv-Q and Send-Q for each connected socket, which is the number of bytes in the queue in each direction.

+4
source

As Eric Ekman replied, the system will buffer a certain amount of data, even if you are not reading it boring.

After the buffer becomes more full, the TCP / IP implementation of the recipient will reduce the size of the allowed recipient size, as a result of which the peer will send smaller pieces of data, effectively throttling the transmission. When the buffer is full, the window size drops to zero, and the peer is allowed to send additional data. Even if this happens, the data will not be lost because the peer will resume sending packets after flushing the receive buffer.

A proper TCP / IP implementation ensures that no data is simply lost on skipping - the connection is either reliable, working, or completely lost, as indicated by read return -1.

-1
source

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


All Articles