int Select (
int Nfds, //ignore fd_ser* Rea DFDS, //points to a socket collection that detects its readability fd_set* Writefds, //points to a socket combination to detect its writable & nbsp fd_ser* Exceptfds,//point to a socket collection to detect errors const struct Timeval * Timeout //Specify this The maximum time the function waits, and if NULL, the maximum time is infinitely large. ); parameter Description: (1) Nfds windows not used, this parameter is used under Linux (2) readfds: Socket send in Readfds collection any one of the following three cases is considered readable, and the Select function returns to remain in the Readfds collection, and the unreadable Readfds collection is removed. 1. The socket has data readable, you can call the RECV function on the socket to receive data 2. The socket connection is closed, restarted, or interrupted, the socket should be closed 3.listen is called, and there is a connection pending, call the Accept function on the listen socket to establish a new link. (3) Writefds: The sockets in the Readfds collection are considered writable in the following three scenarios 1. Use Connect sockets to establish a link for the first time 2. Using the Accept socket is received 3. Using the send operation fails, returns a Wsaewouldblock error, and the buffer space becomes available send out the data is actually first It exists in the send buffer of Winsock before it is sent out, and if the buffer is full, then call Send (Wsasend,sendto,wsasendto) will return a wsaewouldblock error code, Next, as the data in the send buffer is sent out, when there is free space in the buffer, a Fd_write event is triggered, and it is easy to confuse the Fd_wThe premise of the RITE trigger is that the buffer is filled first and then the free space is present as the data is sent, instead of having free space in the buffer (4) Exceptfds (not used, No in-depth study) 1. If a non-blocking connection call is being processed, the connection view fails 2.OOB data readable (5) Timeout: Set the time, if the set time is exceeded, and no network event occurs, returns 0 if this parameter is NULL, Select waits indefinitely until a description word satisfies the condition. Timeout points to a timeval structure typedef struct timeval{ long tv_sec; //Just wait a few seconds long tv_ USec //indicates how many milliseconds to wait} timeval; if Timeval is {0,0}, select () returns immediately, which can be used to inquire about the state of the selected socket interface. If you are in this state, the select () call can be considered non-blocking, and all assumptions applicable to non-blocking calls apply to it specific programming process 1. Initialize Socket collection Fdsocket, Add a listener socket to this collection 2. Copy the Fdsocket collection to Fdread to the Select function, and when there is time to send, the Select function removes the socket with no pending IO operations in the Fdread Collection 3. Compare the Fdread collection after the original Fdsocket collection and select Processing. Determine which sockets have pending IO4. Go back to the 2nd step and continue processing.
My_socket (); My_bind (port); My_listen ();//postmessage (h_hand,wm_user_threadend,0,0);//select model processing process//(1). Initializes the socket collection Fdsocket. Add a listener socket handle to this set Fd_zero (&fdsocket); Fd_set (Ssock,&fdsocket); while (1) {//(2.) Passes a copy of the Fdsocket collection fdread to the Select function//When time occurs, the SELECT function has a socket handle in the Fdread collection that does not have a pending IO operation, and then returns. Fd_set Fdread = Fdsocket;int nret = select (0,&fdread,null,null,null); The timeout parameter controls when the select () completes. If the timeout parameter is a null pointer, select () will block//until a description word satisfies the condition. Otherwise, timeout points to a timeval structure that specifies how long the Select ()//Call Waits before returning//fdwrite 1. After the successful socket is first established, the C/s terminal will trigger a Fd_write event//2, the precondition is that the buffer is filled and then the available space will appear as the data is sent if (nret>0) {//(3) Compare//Determine which sockets have pending IO and further process these iofor (int i=0;i< (int) fdsocket.fd_count;i++) {if (FD) by comparing the original Fdsocket collection with the Fdread collection after select processing. _isset (Fdsocket.fd_array[i],&fdread)) {if (fdsocket.fd_array[i] = Ssock)//(1) Listener socket received new connection, there is a new link {if (fdsocket.fd_ Count<fd_setsize)//Judging the collection is full? {int socke_len = sizeof (REMOTEADDR);//4.acceptsocket Csock = Accept (Ssock, (sockaddr*) &remoteaddr,&socke_len) if (Csock = = invalid_socket) {afxmesSagebox ("Accept failed!\n");p rintf ("Accept failed!\n"); continue;} Fd_set (Csock,&fdsocket);//printf ("received a connection request! :%s\r\n ", Inet_ntoa (REMOTEADDR.SIN_ADDR));//printf (" Client currently connected to the server has%d \ n ", fdsocket.fd_count+1); socket_id = csock* (- 1); PostMessage (h_hand,wm_user_threadend,0,0);} Else{afxmessagebox ("Too Much Connections!\n");p rintf ("Too much connections \ n"); continue;}} Else{int nrecv = recv (fdsocket.fd_array[i],readtext,sizeof (ReadText), 0); socket_id = Fdsocket.fd_array[i];if (nRecv >0)//(2) readable {readtext[nrecv] = ' + ';//hwnd g_windowhandle= ((CDialog *) AfxGetMainWnd ())->getsafehwnd (); PostMessage (h_hand,wm_user_threadend,0,0);} else//(3) connection shutdown, restart or interrupt {closesocket (fdsocket.fd_array[i]); FD_CLR (fdsocket.fd_array[i],&fdsocket); someone_out = TRUE; PostMessage (h_hand,wm_user_threadend,0,0);}}}} Else{afxmessagebox ("failed select ()]n");p rintf ("failed select ()]n");
These are just some of the code that I use in my project, and the first My_socket,my_bind,my_listen are the packages that I've re-made for socket,bind,listen myself.
The Windows socket programming Select model uses