The Select of socket communication function in C + +

Source: Internet
Author: User
Tags set set

The Select function determines the state of one or more sockets (socket) and waits for asynchronous I/O if needed.

int Select (

__in int Nfds,

__inout Fd_set *readfds,

__inout Fd_set *writefds,

__inout Fd_set *exceptfds,

__int const struct Timeval *timeout

);

Parameters :

Nfds: ignored.

Readnfds: An optional pointer to a collection of sockets that checks readability.

Writefds: An optional pointer to a collection of sockets that checks for a writable character.

Exceptfds: An optional pointer to the set of sockets to check for errors.

The maximum time that the Timeout:select function needs to wait, and it needs to be provided in a timeval structure, and for blocking operations, this parameter is null.

return value :

The Select function returns the total number of sockets that are prepared and contained in the FD_SET structure, returns 0 if the timeout occurs, or returns SOCKET_ERROR if the error occurred. If the return value is SOCKET_ERROR, the specified error code can be retrieved by the WSAGetLastError function.

Error code

Explain

wsanotinitialisted

Before using this function, the WSAStartup function must be successfully executed

Wsaefalut

Unable to allocate required resources or READFDS, Writefds, Exceptfds, timeval parameters when the socket executes is not part of the user's address space.

Wsaenetdown

Network subsystem failure

Wsaeinval

The timeout value is not valid, or the other three parameters are empty.

Wsaeintr

Blocked socket 1.1 Call canceled via WSACancelBlockingCall

Wsaeinprogress

The blocking socket 1.1 call is processing or the service provider is processing a drop-off user function.

Wsaenotsock

The description set includes an entry that is not a socket.

Description

The Select function is used to determine the state of one or more sockets. For each socket, the caller can request read, write, or error state information. A set of sockets that requests a given state is specified by the fd_set struct. Sockets in the FD_SET structure must be associated with a single service provider. Based on this, if the WSAPROTOCOL_INFO structure has the same ProviderID value, the socket is considered to be from the same service provider. Until returned, the structure is updated to reflect the specified conditional socket subset. The Select function returns the number of sockets that satisfy the condition. The Fd_set collection can be manually manipulated by some macros. These macros are also suitable for Berkeley sockets, but their mechanisms are fundamentally different.

Parameter Readfds indicates that the readability of the socket is checked. When a socket is in the listen state, if a connection request has been received, the socket is marked as readable, for example a accept will ensure that the completion is not blocked. For other sockets, readability means that the data in the queue is suitable for reading, and will not block after calling Recv,wsarecv,wsarecvfrom or Recvfrom.

For connection-oriented sockets, readability can also indicate that a request received from the other end of a socket is closed. If the virtual circuit shuts down properly and all the data has been received, then Recv will return immediately (no data is received), and if the virtual circuit is reset, Recv will immediately return the error code, such as Wsaeconnreset. If the socket option So_oobinline (see Setsockop), the OOB data that appears will be checked.

The parameter Writefds indicates that the socket is checked for its writable character. If the socket handles connect calls (non-blocking) and fully establishes the connection, the socket is writable. If the socket does not handle the connect call, the writable nature means that the guarantee send,sendto or wsasendto execution succeeds. However, if the Len parameter exceeds the system's cache space size, they can be blocked in blocking sockets. It is legal to be unsure how long the length is, especially in multi-threaded environments.

The parameter Exceptfds indicates that the socket is checked for OOB data or an abnormal error environment.

Note: The OOB data is only applied when the So_oobinline is set to false. If a socket handles a connection call (non-blocking mode), the error message attempting to connect is in Exceptfds, and the document does not define which errors need to be included.

Any two parameters in Readfd,writefds or Exceptfds need to be null when invoked. At least one must be non-null, and any Non-empty description setting must include at least one socket handle.

In short, a socket will be specified in a special set when the select returns if:

Readfds:

① If the Listen function has already been invoked and the connection is suspended, accept will perform successfully.

② data is suitable for reading (if So_oobinline position, including OOB data)

③ connection is off/reset/terminated

Writefds:

① If you handle a connect call (non-blocking), the connection succeeds.

② data can be sent.

Exceptfds:

① If you are handling a connect call (non-blocking), the connection fails.

②oob data is suitable for reading (only if So_oobinline is not set)

Define four macros in the header file Winsock2.h to manipulate and examine the description set. Fd_setsize determines the maximum number in the description set (the default value of Fd_setsize is 64, which can be modified by fd_setsize before the Winsock2.h is imported).

These macros are used to maintain software convenience in different socket environments. These macro actions and check fd_set content are:

fd_clr (s, *set)

Remove descriptor s from set collection

Fd_isset (s, *set)

If S is in set, returns non 0, otherwise returns 0

Fd_set (s, *set)

Add descriptor s to set

Fd_zero (*set)

Initializes a set set to a null collection

The parameter time-out controls when the Select function finishes (more than this time returns a timeout). If time-out is a null pointer, the select remains blocked to instruct at least one descriptor to conform to the specified criteria. Otherwise, time-out points to a timeval struct body that specifies that the select should wait for the maximum time before returning. When the select returns, the contents of the TIMEVAL structure are not changed. If Timeval is initialized to {0,0},select returns immediately; this is used to get the status of the selected socket. If the select returns immediately and the select call is considered non-blocking, then the standard assumption for non-blocking calls applies. For example, blocking hooks will not be invoked and form sockets will not exit.

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.