For the select model of windows socket programming, socketselect

Source: Internet
Author: User
Tags connect socket

For the select model of windows socket programming, socketselect


Int select (

Int nfds, // ignore fd_ser * readfds, // point to a socket set, used to detect its readability fd_set * writefds, // point to a socket combination, used to detect the write-ability fd_ser * into TFDs, // points to a socket set, used to detect the const struct timeval * timeout // specifies the maximum waiting time of this function. If it is NULL, the maximum time is infinite .); Parameter description: (1) nfds is not used in windows. this parameter is used in linux (2) readfds: in any of the following three cases, sockets in the readfds set are considered readable. The select function returns the result and remains in the readfds set. unreadable sockets are removed from the readfds set. 1. the socket has data readable and can call the recv function on the socket to receive data. the socket connection has been closed, restarted, or interrupted. At this time, the socket should be closed. 3. the listen is called, and a connection is pending. Call the accept function to create a new link for the listen socket. (3) writefds: sockets in the readfds set are considered writable in the following three cases. create a connection for the first time using connect socket 2. use the accept socket to receive 3. if the send operation fails, the WSAEWOULDBLOCK error is returned, and the buffer space becomes available for sending data. In fact, the data is stored in the winsock sending buffer before being sent out. If the buffer zone is full, then, if you call send (WSASend, sendto, WSASendTo), a WSAEWOULDBLOCK error code will be returned. When the data in the sending buffer is sent out, when the buffer zone contains available space, A FD_WRITE event will be triggered. What is confusing here is that the FD_WRITE trigger requires the buffer to be filled first and then the available space appears as data is sent, instead of having space in the buffer (4) using TFDs (not used, not studied in depth) 1. if a non-blocking connection call is being processed, connection View Failed 2. OOB data readable (5) timeout: set the time. If no network event occurs after the specified time is exceeded, 0 is returned. If this parameter is NULL, select will wait infinitely, until a descriptive word meets the conditions. Timeout points to a timeval structure typedef struct timeval {long TV _sec; // only the number of seconds to wait long TV _usec; // indicates the number of milliseconds to wait} timeval; if the timeval is {0, 0 }, select () returns immediately, which can be used to query the status of the selected interface. If it is in this state, the select () call can be considered non-blocking, and all the assumptions applicable to non-blocking calls apply to its specific programming process 1. initialize the socket set fdSocket and add a listening socket to this set. 2. copy the fdSocket set to fdRead and pass it to the select function. When there is time for sending, the select function removes the socket in the fdRead set without pending io operations. 3. compare the fdRead set processed by the fdSocket set with the select statement. Determine which sockets have pending IO4. Go Back To Step 4 to continue processing.
My_socket (); my_bind (port); my_listen (); // PostMessage (h_hand, WM_USER_THREADEND, 0, 0); // select model processing process // (1 ). initialize the socket set fdSocket. add the listening socket handle to this collection FD_ZERO (& fdSocket); FD_SET (sSock, & fdSocket); while (1) {// (2 .) pass a copy of The fdSocket set fdRead to the select function // when there is time, the select function is a socket handle in the fdRead set without pending IO operations, and then return. Fd_set fdRead = fdSocket; int nRet = select (0, & fdRead, NULL); // The timeout parameter controls the select () completion time. If the timeout parameter is a null pointer, select () will always block // until a descriptive word meets the condition. Otherwise, timeout points to a timeval structure, specifying the select () // how long the call will wait before the return // fdwrite 1. when a successful SOCKET establishes a connection for the first time, C/S will trigger an FD_WRITE event // 2, the prerequisite for triggering is that the buffer zone must be filled first and then the available space appears as data is sent. if (nRet> 0) {// (3) compare the fdSocket set with the fdRead set after select processing // determine which sockets have pending io and further process these iofor (int I = 0; I <(int) fdSocket. fd_count; I ++) {if (FD_ISSET (fdSocket. fd_array [I], & fdRead) {if (fdSocket. fd_array [I] = sSock) // (1) the listener socket receives a new connection with a new link {if (fdSocket. fd_count <FD_SETSIZE) // determines if the set is full? {Int socke_len = sizeof (remoteAddr); // 4. acceptSOCKET cSock = accept (sSock, (SOCKADDR *) & remoteAddr, & socke_len); if (cSock = INVALID_SOCKET) {AfxMessageBox ("accept failed! \ N "); printf (" accept failed! \ N "); continue;} FD_SET (cSock, & fdSocket); // printf (" receives a connection request!: % S \ r \ n ", inet_ntoa (remoteAddr. sin_addr); // printf ("the number of clients currently connected to the server is % d \ n", fdSocket. fd_count + 1); socket_id = cSock * (-1); PostMessage (h_hand, WM_USER_THREADEND, 0, 0);} else {AfxMessageBox ("too much connections! \ N "); printf (" 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] = '\ 0'; // HWND g_1_whandle = (CDialog *) afxGetMainWnd ()-> GetSafeHwnd (); PostMessage (h_hand, WM_USER_THREADEND,);} else // (3) Connection closed, 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 "); printf (" failed select ()] n "); break ;}}

 

The above is only part of the code I used in the project. The initial my_socket, my_bind, and my_listen are self-encapsulated for socket, bind, and listen.

 

 

Related Article

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.