overall, the role of the Select function:
Determine the state of one or more sets of interfaces, this function is used to determine the state of one or more sets of interfaces, for each set of interfaces, the caller can query its readability, writable and error status information, with Fd_set Structure to represent a set of interfaces waiting to be inspected, when the call returns, the structure has a subset of the set of interface groups satisfying certain conditions , and select () returns the number of socket interfaces that satisfy the condition.
In simple terms, select is used to populate a set of available socket handles when one of the following conditions is true:
1. Sockets that can be read. When these sockets are returned, operations such as recv/accept on these sockets do not cause blocking;
2. Sockets that can be written. When these sockets are returned, performing a send on these sockets will not cause blocking;
3. Return the sockets with the error.
the processing functions of select are generally:
also used with select pairing are:
FD_CLR (S, *set)
removes the descriptor s from set.
Fd_isset (S, *set)
nonzero if S is a member of the set. Otherwise, Zero.
Fd_set (S, *set)
Adds descriptor S to set.
Fd_zero (*set)
initializes the set to the null set.
It should be known that the Accept () function is a blocking function that is returned when a client requests a connection to the server. As the following example
Server side:
/span>
#include <iostream> #include <windows.h>using namespace std; #define PORT 4000#define ip_address "127.0.0.1" #pragma comment (lib, "Ws2_32.lib") int main (int argc, char* argv[]) {wsadata Ws; SOCKET ServerSocket, Cientsocket;struct sockaddr_in localaddr, clientaddr;int Ret = 0;int Addrlen = 0; HANDLE hthread = null;//init Windows Socketif (WSAStartup (Makeword (2, 2), &WS)! = 0) {cout << "Init windows Socke T Failed:: "<< GetLastError () << endl;return-1;} ServerSocket = socket (af_inet, Sock_stream, ipproto_tcp), if (ServerSocket = = invalid_socket) {cout << "Create Socket Failed:: "<< GetLastError () << endl;return-1;} localaddr.sin_family = af_inet; LOCALADDR.SIN_ADDR.S_ADDR = inet_addr (ip_address); Localaddr.sin_port = htons (port); memset (Localaddr.sin_zero, 0x00, 8); Ret = Bind (ServerSocket, (struct sockaddr*) &localaddr, sizeof (LOCALADDR)); if (Ret! = 0) {cout << bind Socket Fai LED:: "<< GetLastError () << endl;return-1;} Ret = LIsten (ServerSocket), if (Ret! = 0) {cout << "Listen Socket Failed::" << GetLastError () << Endl;return -1;} cout << "Server has been started" << Endl;char Recvbuffer[max_path];while (true) {Addrlen = sizeof (CLIENTADDR); Cientsocket = Accept (ServerSocket, (struct sockaddr*) &clientaddr, &addrlen);//Blocked if (Cientsocket = = Invalid_ SOCKET) {cout << "Accept Failed::" << GetLastError () << Endl;break;} cout << "Client connection::" << Inet_ntoa (CLIENTADDR.SIN_ADDR) << ":" << clientaddr.sin_port << Endl; int Ret = 0;while (True) {memset (Recvbuffer, 0x00, sizeof (Recvbuffer)); ret = recv (Cientsocket, Recvbuffer, MAX_PATH, 0); if (Ret = = 0 | | Ret = = socket_error) {cout << "client exit!" << Endl;break;} cout << "received customer information:" << recvbuffer << Endl;} CloseHandle (hthread);} Closesocket (ServerSocket); closesocket (Cientsocket); WSACleanup (); return 0;}
Client side:
#include <iostream> #include <Windows.h> #pragma comment (lib, "Ws2_32.lib") using namespace std; #define PORT 4000#define ip_address "127.0.0.1" int main (int argc, char* argv[]) {wsadata Ws; SOCKET cientsocket;struct sockaddr_in serveraddr;int Ret = 0;int Addrlen = 0; HANDLE hthread = Null;char sendbuffer[max_path];//init Windows socketif (WSAStartup (Makeword (2, 2), &WS)! = 0) {cout &L t;< "Init Windows Socket Failed::" << GetLastError () << endl;return-1;} Create socketcientsocket = socket (af_inet, sock_stream, ipproto_tcp); if (Cientsocket = = invalid_socket) {cout << "Create Socket Failed::" << GetLastError () << endl;return-1;} serveraddr.sin_family = af_inet; SERVERADDR.SIN_ADDR.S_ADDR = inet_addr (ip_address); Serveraddr.sin_port = htons (port); memset (Serveraddr.sin_zero, 0x00, 8); ret = Connect (cientsocket, (struct sockaddr*) &serveraddr, sizeof (SERVERADDR)); if (Ret = = socket_error) {cout < < "Connect Error::" << GetLastError () << endl;return-1;} Else{cout << "connected successfully!" << Endl;} while (true) {cin.getline (Sendbuffer, sizeof (Sendbuffer)); ret = Send (Cientsocket, sendbuffer, (int) strlen (Sendbuffer), 0); if (Ret = = socket_error) {cout << "send Info ERROR::" << GetLastError () << Endl;break;}} Closesocket (Cientsocket); WSACleanup (); return 0;}
The above example a client can only receive requests to process a client!
With the application of select, you can have one server handle requests from multiple clients:
server side after overwriting
#include <WinSock2.h> #include <stdio.h> #pragma comment (lib, "Ws2_32.lib") #define PORT 4000bool Initandlistern (SOCKET &slisten) {wsadata wsadata;sockaddr_in local; WORD Version = Makeword (2, 0), int ret = WSAStartup (version, &wsadata), if (ret! = 0) {printf ("Wsastarup failed\n"); retur n 0;} local.sin_family = af_inet;local.sin_addr.s_addr = Inaddr_any;local.sin_port = Htons ((u_short) port); SListen = socket ( Af_inet, Sock_stream, 0); if (Slisten = = Invalid_socket) {printf ("Initial SOCKET failed\n"); return 0;} if (Bind (Slisten, (sockaddr*) &local, sizeof (local)) = 0) {printf ("bind socket failed\n"); return 0;} if (Listen (slisten)! = 0) {printf ("Listen socket failed\n"); return 0;} return 1;} int main () {SOCKET slisten;if (initandlistern (slisten) = = 0) return 0;printf ("Server Wait for client connect...\n"); fd_set Fdsocket; Fd_zero (&fdsocket); Fd_set (Slisten, &fdsocket);//Add Slisten into the collection while (true) {Fd_set Fdread = fdsocket;int nret = Select (NULL, & Fdread, NULL, NULL, NULL);if (nret <= 0) break;for (int i = 0; i < (int) Fdsocket.fd_count; ++i) {if (Fd_isset (fdsocket.fd_array[i), &fdread) {if (fdsocket.fd_array[i] = = Slisten) {sockaddr_in addrremote;int Naddrlen = sizeof (addrremote); SOCKET snew =:: Accept (Slisten, (sockaddr*) &addrremote, &naddrlen); Fd_set (Snew, &fdsocket);p rintf ("Clietn%s connected\n", Inet_ntoa (ADDRREMOTE.SIN_ADDR));} Else{char buffer[1024];memset (buffer, 0, 1024x768), int nrecev = recv (Fdsocket.fd_array[i], buffer, 1024x768, 0); if (Nrecev > 0) {printf ("Received Client msg:%s\n", buffer); Send (Fdsocket.fd_array[i], buffer, strlen (buffer), 0);} Else{closesocket (Fdsocket.fd_array[i]); FD_CLR (Fdsocket.fd_array[i], &fdsocket);}}}} return 0;}
The use of the WinSocket Select function (comparison study of Windows Sockets)