Prerequisites for the select function file descriptor set

Source: Internet
Author: User
Tags connect socket socket error

In network programming, we often discuss waiting for a descriptor to prepare I/O (read/write) or waiting for an exception condition to be processed on it. Although readability and writability are obvious to descriptors such as common files, we must be more specific about the conditions that cause the socket "ready" returned by the SELECT statement.

Socket select function prototype:

# Include <sys/select. h> # include <sys/time. h> int select (INT maxfdp1, fd_set * readset, fd_set * writeset, fd_set * sorted tset, const struct timeval * timeout); // maxfdp1: specifies the number of descriptors to be tested, its value is generally the maximum descriptor to be tested plus 1; // the three parameters in the middle are the descriptor of the kernel test read, write, and exception condition; // return: if there is a ready descriptor, it is the number. If it times out, It is 0. If an error occurs, it is-1;


Read conditions for the socket:

A) The number of data bytes in the socket accept buffer is greater than or equal to the current size of the low-level mark in the socket accept buffer. Executing read operations on such sockets will not block and a value greater than 0 will be returned (that is, the data to be read is returned ). We can use the so_rcvlowat socket option to set the low-level mark of the socket. For TCP and UDP sockets, the default value is 1.

B) Close the read half of the socket (that is, accept the TCP connection of fin ). Read operations on such sockets will not be blocked and 0 will be returned (that is, EOF will be returned)

C) This socket is a listening socket and the number of established connections is not 0. The accept for such a socket is usually not blocked. (You can post a document later to introduce a timing condition for blocking accept)

D) There is a socket error to be processed. The read operation on such a socket will not block and return-1 (that is, an error is returned), and errno is set to an exact error condition. In this way, pending error can also be obtained and cleared by calling getsockopt by specifying the so_error socket option.

Prepare write conditions for the socket:

A) The number of available space bytes in the socket sending buffer is greater than or equal to the current size marked by the low level of the socket sending buffer, or the socket is connected, or the socket does not need to be connected (such as UDP socket ). This means that if we set such a socket to non-blocking, the write operation will not block and return a positive value (such as the number of bytes accepted by the transport layer ). We can use the so_sndlowat socket option to set the low-level mark of the socket. For TCP and UDP, its default value is usually 2048.

B) The write part of the connection is closed. Write operations on such a socket will generate a sigpipe signal.

C) The non-blocking connect socket has established a connection, or the connection has ended in failure.

D) There is a socket error to be processed. Write operations on such a socket will not be blocked and return-1 (that is, an error is returned), and errno is set to an exact error condition. You can also call getsockopt to obtain and clear these pending errors by specifying the so_error socket option.

Note: When an error occurs on a socket, it marks select as readable and writable. In addition, any UDP socket is always writable as long as its low-level mark is smaller than or equal to the sending buffer size, because UDP socket does not need to be connected.

Prerequisites for the select function file descriptor set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.