Windows network and communication program design-select model

Source: Internet
Author: User

Select model

Specific programming process:

1. initialize the socket set fdSocket and add a listening socket to the set.

2. Copy the fdSocket set to a fdRead set to be read temporarily, and then pass fdRead to the select function. When an event occurs
The select function removes the socket handle that has no pending I/O operations in the set and returns the result.

3. Compare the fdRead set processed by the fdSocket set and select, determine which sockets have pending I/O, and further process these I/O.

4. Go Back To Step 1 to continue processing.


# Define _ WIN32_WINNT 0x0400 # include <windows. h> # include <cstdio> # include "InitSocket. h "CInitSock initSock; // before entering the main function, int main (void) {USHORT nPort = 4567; // The port number listened by the server // create the SOCKET socket sListen = SOCKET (AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in sin; sin. sin_family = AF_INET; sin. sin_port = htons (nPort); sin. sin_addr.s_addr = INADDR_ANY; // bind the socket to the local machine if (bind (sListen, (sockaddr *) & sin, sizeof (s) In) = SOCKET_ERROR) {printf ("Failed bind () \ n"); return 0 ;}// enter the listening mode listen (sListen, 5 ); // select model processing process // initialize a socket set fdSocket and add the listening socket handle to this set fd_set fdSocket; // all available socket sets FD_ZERO (& fdSocket); FD_SET (sListen, & fdSocket); while (TRUE) {// 2 transfers a copy of The fdSocket set fdRead to the select function // when an event occurs, the select function removes the socket handle that has no pending I/O operations in the fdRead set and returns the result. Timeval reportTime; reportTime. TV _sec = 5; reportTime. TV _usec = 5000; fd_set fdRead = fdSocket; int nRet = select (0, & fdRead, NULL, NULL, & reportTime); if (nRet> 0) {// determine which sockets have pending I/O, and further process these I/O // by comparing the original fdSocket set with the fdRead set processed by select for (int I = 0; I <(int) fdSocket. fd_count; ++ I) {if (FD_ISSET (fdSocket. fd_array [I], & fdRead) {if (fdSocket. fd_array [I] = sListen) // The Listener socket receives the new connection {if (fdSocket. fd_count <FD_SETSI ZE) {sockaddr_in addrRemote; int nAddrLen = sizeof (addrRemote); SOCKET sNew = accept (sListen, (SOCKADDR *) & addrRemote, & nAddrLen); FD_SET (sNew, & fdSocket ); printf ("received connection (% s) \ n", inet_ntoa (addrRemote. sin_addr);} else {printf ("Too much connectinos! \ N "); continue;} else // read event. The end event is also {char szText [256]; // int nRecv = recv (fdSocket. fd_array [I], szText, strlen (szText), 0); int nRecv = recv (fdSocket. fd_array [I], szText, sizeof (szText), 0); if (nRecv> 0) // readable {szText [nRecv] = '\ 0 '; printf ("received data: % s \ n", szText);} else // Connection closed, restarted, or interrupted {printf ("close a connection \ n "); closesocket (fdSocket. fd_array [I]); FD_CLR (fdSocket. fd_array [I], & fdSocket) ;}}} else if (0 = nRet) // used for testing, check whether the server can read the client Exit message {printf ("Time Out: % d \ n", fdSocket. fd_count); continue;} else {printf ("Failed select () \ n"); break ;}} return 0 ;}

Summary:

The benefit of using the select model is that multiple socket connections can be processed simultaneously in a single thread, which avoids the thread expansion problem in the blocking model. However, the number of sockets added to the fd_set structure is limited. By default, the maximum value of FD_SETSIZE is defined as 64 in the winsock2.h file. To increase the number of sockets, the application can define FD_SETSIZE as a larger value (this definition must appear before winsock2.h ). However, the custom value cannot exceed the limit of the lower-level Winsock provider (usually 1024 ). (Or use multiple working threads, select another 64 sockets)

In addition, if the value of FD_SETSIZE is too large, the server performance will also be affected. For example, if there are 1000 sockets, you must set these 1000 sockets before calling the select statement. After the select statement is returned, you must check the 1000 sockets.


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.