Prototype
int select(int nfds,fd_set* readfds,fd_set* writefds,fd_set* exceptfds,const struct timeval* timeout);
The first parameter NFDs
Description in Linux: specify the maximum value of the test descriptor, Which is tested from 0 to NFDs,
In Windows: ignored.NFDsParameter is wrongly ded only for compatibility with Berkeley sockets. ignored.
Start with accept.
First:
Socket sock; sock = socket (af_inet, sock_stream, 0); struct sockaddr_in ADDR; // tell sock where to licencememset (& ADDR, 0, sizeof (ADDR )); ADDR. sin_family = af_inet; ADDR. sin_port = htons (11111); // port ADDR. sin_addr.s_addr = htonl (inaddr_any); // start listening to bind (sock, (sockaddr *) & ADDR, sizeof (ADDR) on all IP addresses of the local machine )); // bind .... listen (sock, 5); // a maximum of five queue socket socka; // This is used to accept a connection fd_set RfD; // descriptor set this will be used to test whether there is an available connection struct timeval timeout; fd_zero (& RFD); // always first clears a descriptor set timeout. TV _sec = 60; // This timeout is used in select. TV _usec = 0; u_long ul = 1; ioctlsocket (sock, fionbio, & UL); // non-blocking connection
// Selectfd_set (sock, & RFD) is used now ); // put sock into the descriptor set to be tested, that is, put sock into RfD so that the sock will be tested during the next call to select to test RfD (because we put sock into the RDF) a descriptor set can contain multiple tested descriptors. If (select (sock + 1, & RfD, 0, & timeout) = 0) // The first parameter of select can be ignored (in this way, // write to be consistent with that in Linux) the second parameter is placed in the read descriptor set to be tested (that is to say, if there is a // descriptor that can be read, select will return) and the third parameter is placed in the write descriptor set to be tested, the fourth entry is the "executable // row descriptor set "(?? I do not know) The fifth parameter is the time-out time (if the time-out time still does not have the descriptor // ready, select also returns. (if it is null, it will wait until a descriptor set is ready.) {// This braces are connected, if the return value is 0, the specified Timeout time is exceeded. // processing ....} if (fd_isset (sock, & RFD) {// There is a descriptor prepared socka = accept (sock,); // accept it.
// You need to determine whether socka is a valid socket ....
Bytes -------------------------------------------------------------------------------------------------------------------------------
Generally
Assume that you want to determine whether two sockets are readable and writable:
Assume that socka and sockb are two sockets that have been connected and can send and receive data.
Fd_set RfD, WFD; // one is used to test and read one is used to test and write fd_zero (& RFD); fd_zero (& WFD); fd_set (socka, & RFD ); // put socka into the read descriptor set fd_set (sockb, & RFD); // put sockb into the read descriptor set fd_set (socka, & WFD ); put socka into the write descriptor set fd_set (sockb, & WFD); Put sockb into the write descriptor set if (socket_error! = Select (0, & RfD, & WFD,) // test the two descriptor sets, RFD is only used to test read WFD. It is only used to test write {// No error if (fd_isset (socka, & RFD) // socka readable {...} if (fd_isset (sockb, & RFD) // sockb readable {...} if (fd_isset (socka, & WFD) // socka can be written {...} if (fd_isset (sockb, & WFD) // sockb can write {...}}