How to use Select model and ioctlsocket

Source: Internet
Author: User
Tags readable socket socket blocking
How to use Select model and Ioctlsocket

2011-03-29 11:38:26|  Category: Software development C + + | Tags: ioctlsocket socket blocking Select | Font size Big Small subscription

int iMode = 1; 0: Blocking
Ioctlsocket (Socketc,fionbio, (U_long far*) &imode);//non-blocking settings

Rs=recvfrom (Socketc,rbuf,sizeof (RBUF), 0, (sockaddr*) &addr,&len);


int ioctlsocket (SOCKET s, long cmd, u_long far* ARGP); s [in] a descriptor identifying a socket. CMD [in] the command to perform on the socket s. ARGP [in/out] A pointer to a parameter for CMD.

Do not know if you have encountered this situation, when the socket TCP connection (that is, call Connect), once the network is not available, or the IP address is invalid, it may cause the entire thread to block. It's usually 30 seconds (I'm measuring 20 seconds). If set to non-blocking mode, this problem can be solved well, we can set the non-blocking mode as follows:

Call the Ioctlsocket function:

unsigned long flag=1;
if (ioctlsocket (Sock,fionbio,&flag)!=0)
{
Closesocket (sock);
return false;
}

The following is an explanation of the ioctlsocket function:
int PASCAL far ioctlsocket (SOCKET s, long cmd, u_long far* ARGP);
S: A descriptive word that identifies the socket interface.
CMD: The operation command for the set interface S.
ARGP: Pointer to the parameter with the cmd command.


Comments:
This function can be used for any set of interfaces in any state. It is used to obtain the operation parameters associated with the socket, regardless of the specific protocol or communication subsystem. The following commands are supported:
Fionbio: Allows or disables the non-blocking mode of the socket s. ARGP points to an unsigned long integer. Non-zero if non-blocking mode is allowed, or zero if non-blocking mode is disabled. When a set of interfaces is created, it is in blocking mode (that is, non-blocking mode is disabled). This is consistent with the BSD socket interface. The Wsaasynselect () function automatically sets the socket interface to non-blocking mode. If a wsaasynselect () operation has been performed on a set of interfaces, any attempt to set the socket interface back to blocking mode with ioctlsocket () will fail with Wsaeinval. In order to reset the socket to block mode, the application must first use the Wsaasynselect () call (the IEvent parameter is set to 0) to disable the Wsaasynselect ().

Fionread: Determines the amount of data that the set interface s automatically reads. ARGP points to an unsigned long integer that contains the return value of ioctlsocket (). If S is the Socket_stream type, Fionread returns all the amount of data received in a single recv (). This is usually the same amount of data that is queued in the set interface. If S is a sock_dgram type, then Fionread returns the first datagram size queued on the set interface.
Siocatmark: It is true that all out-of-band data has been read into. This command applies only to the Sock_stream type of socket, and the socket is set to be able to receive out-of-band data (So_oobinline) online. If no out-of-band data is waiting to be read, the operation returns True. Otherwise, return false, the next recv () or recvfrom () operation will retrieve some or all of the data before "Mark". The application can use the Siocatmark action to determine if there is any data left. If there is regular data before the "emergency" (out-of-band) data, the data is received sequentially (note that the recv () and recvfrom () operations do not confuse regular data with out-of-band data in a single call). ARGP points to a bool type number, where ioctlsocket () deposits the return value.
The nonblocking mode is already set, but the connection time for connect is not set, and we can do this by invoking the SELECT statement. The following code sets the connection time to 5 seconds and returns directly if it is not yet connected.
struct Timeval timeout;
Fd_set R;
int ret;
Connect (sock, (LPSOCKADDR) sockaddr, Sockaddr.size ());
Fd_zero (&R);
Fd_set (SOCK,&R);

Timeout.tv_sec = 5;
timeout.tv_usec = 0;
ret = select (0,0,&r,0,&timeout);

if (ret <= 0)
{
Closesocket (sock);
return false;
}
The following is an explanation of the Select function:
int Select (
int Nfds,
Fd_set FAR * Readfds,
Fd_set FAR * Writefds,
Fd_set FAR * Exceptfds,
const struct TIMEVAL FAR * Timeout
);
The first parameter, Nfds, is not useful and is provided only for compatibility with Berkeley sockets.
READFDS specifies a socket array (which should be one, but is mainly represented as a socket array), select Checks all sockets in the array. If returned successfully, the Readfds contains an array member that meets the ' readability ' criteria, such as having readable data in the buffer.
WRITEFDS specifies a socket array, select Checks all the sockets in the array. If returned successfully, the WRITEFDS contains an array member that conforms to the ' writable ' condition (such as a successful connection).
EXCEPTFDS specifies a socket array, select Checks all the sockets in the array. If returned successfully, the Cxceptfds contains an array member that meets the ' exception ' condition (such as a connection failure).
Timeout Specifies the maximum time that select executes, and returns 0 if the socket specified in Readfds, Writefds, and Exceptfds does not meet the requirements within the timeout limit.
If you make a non-blocking call to connect, readable means that the connection is successful and the connection is not readable. So with this setting, we are able to make changes to the Connect connection time. However, it should be noted that such a setting does not guarantee that the network will not connect in a limited time period. For example, we set the time is 5 seconds, but for a variety of reasons, may be able to connect in the first 6 seconds, but the function is returned after 5 seconds.

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.