Reputation: 16121
What does ssize_t read(int fd, void * data, size_t count);
exactly do?
In a lot of articles in the web is often written, that it tries to read from or the descriptor fd. What does that mean? "It tries" :/ And how is such a socket designed? Does the OS buffer the messages coming in? Or is a read a time critical operation? I mean is there the posibility that some packages get lost, if i dont "read" in time?
EDIT:
I wondered a while now why this is not blocking. Then I wondered why read(...) has other parameters than all functions I saw in code snippets. Finally I realized that it is read(...) not recv(...). Unlucky that it still worked nearly as i expected. And fnuny how our ascostaivie tohuhgts paly geams wtih us. (Don't edit) I have to admit that the example german has more effect on the reader...
Upvotes: 4
Views: 320
Reputation: 155660
As Erik Ekman answered, the system will buffer some amount of data even if you don't read it dilligently.
After the buffer gets more and more filled, the receiver's TCP/IP implementation will reduce the advertised receive window size, causing the peer to send smaller chunks of data, effectively throttling the transfer. When the buffer is full, the window size drops to zero and the peer is allowed to send no additional data. Even if this happens, no data will be lost because the peer will resume sending the packets once the receive buffer is cleared.
A correct TCP/IP implementation guarantees that no data is just lost by skipping—a connection is either reliable and working or it is entirely lost, which is indicated by read
returning -1.
Upvotes: -1
Reputation: 2066
Linux will buffer any data which arrives on a connected TCP socket, up to a few megabytes by default. You dont have to read at the exact same time the data arrives.
netstat -tn
will show Recv-Q
and Send-Q
for each connected socket, which is the number of bytes queued in each direction.
Upvotes: 4