gpt4 book ai didi

c - 套接字文件描述符中的可读/可写是什么意思?为什么普通文件不会为此烦恼?

转载 作者:太空狗 更新时间:2023-10-29 12:02:08 26 4
gpt4 key购买 nike

由于最近刚接触libev,io_watcher中有一个readable/writable的概念不是很懂。据我所知,linux系统编程中有一个参数:

O_ASYNC

A signal (SIGIO by default) will be generated when the specified file becomes readable or writable. This flag is available only for terminals and sockets, not for regular files.

那么,由于常规文件不会为可读/可写而烦恼,那么可读/可写在套接字编程中的真正含义是什么?内核采取什么措施来确定套接字文件描述符是否可读?

考虑到一切皆文件的理念,每个具有不同描述符编号的套接字描述符是否实际上指向同一个文件?如果是这样,我可以认为可读/可写问题是由同步引起的吗?

好吧,看来我问了一个愚蠢的问题。我真正的意思是套接字和常规文件都通过文件描述符读写,所以为什么套接字描述符有一个可读/可写的概念而常规文件没有。由于EJP告诉我这是因为缓冲区和每个描述符都有自己的一对缓冲区,所以我的结论是:可读/可写概念是针对缓冲区的,如果缓冲区为空,则不可读,如果缓冲区已满,则不可写。可读和可写与同步无关,并且由于常规文件没有缓冲区,它始终是可读和可写的。

还有更多的问题:说到receive buffer,这个buffer和int recv(SOCKET socket, char FAR* buf, int len, int flags);不是一回事吧?

最佳答案

这个问题在 Unix Network Programming, Volume 1: The Sockets Networking API (3rd Edition) [W. Richard Stevens、Bill Fenner、Andrew M. Rudoff](see it here。我将添加一些小的编辑以增强可读性):

Under What Conditions Is a Descriptor Ready?

[...] The conditions that cause select to return "ready" for sockets [are]:

1. A socket is ready for reading if any of the following four conditions is true:

  • The number of bytes of data in the socket receive buffer is greater than or equal to the current size of the low-water mark for the socket receive buffer. A read operation on the socket will not block and will return a value greater than 0 (i.e., the data that is ready to be read). [...]
  • The read half of the connection is closed (i.e., a TCP connection that has received a FIN). A read operation on the socket will not block and will return 0 (i.e., EOF).
  • The socket is a listening socket and the number of completed connections is nonzero. [...]
  • A socket error is pending. A read operation on the socket will not block and will return an error (–1) with errno set to the specific error condition. [...]

2. A socket is ready for writing if any of the following four conditions is true:

  • The number of bytes of available space in the socket send buffer is greater than or equal to the current size of the low-water mark for the socket send buffer and either: (i) the socket is connected, or (ii) the socket does not require a connection (e.g., UDP). This means that if we set the socket to nonblocking, a write operation will not block and will return a positive value (e.g., the number of bytes accepted by the transport layer). [...]
  • The write half of the connection is closed. A write operation on the socket will generate SIGPIPE.
  • A socket using a non-blocking connect has completed the connection, or the connect has failed.
  • A socket error is pending. A write operation on the socket will not block and will return an error (–1) with errno set to the specific error condition. [...]

3. A socket has an exception condition pending if there is out-of-band data for the socket or the socket is still at the out-of-band mark.

[Notes:]

  • Our definitions of "readable" and "writable" are taken directly from the kernel's soreadable and sowriteable macros on pp. 530–531 of TCPv2. Similarly, our definition of the "exception condition" for a socket is from the soo_select function on these same pages.

  • Notice that when an error occurs on a socket, it is marked as both readable and writable by select.

  • The purpose of the receive and send low-water marks is to give the application control over how much data must be available for reading or how much space must be available for writing before select returns a readable or writable status. For example, if we know that our application has nothing productive to do unless at least 64 bytes of data are present, we can set the receive low-water mark to 64 to prevent select from waking us up if less than 64 bytes are ready for reading.

  • As long as the send low-water mark for a UDP socket is less than the send buffer size (which should always be the default relationship), the UDP socket is always writable, since a connection is not required.

enter image description here

相关阅读,来自同一本书:TCP socket send buffer and UDP socket (pseudo) send buffer

关于c - 套接字文件描述符中的可读/可写是什么意思?为什么普通文件不会为此烦恼?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com