4

ssize_t read(int fd, void * data, size_t count); 具体是做什么的?

在网络上的很多文章中经常写到,它试图从或描述符 fd 中读取。这意味着什么?“它尝试”:/这样的插座是如何设计的?操作系统是否缓冲传入的消息?或者读取是一个时间关键的操作?我的意思是,如果我不及时“阅读”,是否有一些包裹丢失的可能性?

编辑:

我现在想知道为什么这没有阻塞。然后我想知道为什么 read(...) 有其他参数,而不是我在代码片段中看到的所有函数。最后我意识到它是read (...) 而不是recv (...)。不幸的是,它仍然几乎像我预期的那样工作。有趣的是,我们的 ascostaivie tohuhgts 是如何与我们融为一体的。(不要编辑)我不得不承认德语的例子对读者有更大的影响......

4

2 回答 2

4

Linux 将缓冲到达连接的 TCP 套接字上的任何数据,默认情况下最多为几兆字节。您不必在数据到达的同时阅读。

netstat -tn将为每个连接的套接字显示Recv-QSend-Q,这是每个方向排队的字节数。

于 2012-11-08T21:52:41.280 回答
-1

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.

于 2012-11-08T22:57:36.620 回答