The program written in this project is to communicate with the remote socket, use the accept to accept the long-distance connection request, has been in the program to accept used are blocking mode, received a new connection request, the creation of a new thread processing and client communication tasks. Today, due to the need to implement the server-side set the client heartbeat cycle function, if each thread to query the database in the heartbeat packet cycle has not changed, to achieve too much trouble, instead of using the main thread to query the database stored in the heartbeat packet cycle, because the main thread needs to constantly query the database, So accept can no longer use the blocking way, the internet search a bit, did not find any method, suddenly think of my previous RECEV packet is also used to block the way, later with select to a non-blocking mode, accept can also use Select to achieve non-blocking mode. Try to change the program according to RECEV's non-blocking mode, accept successfully implemented the Non-blocking receive connection request mode.
int maxfdp=null;
struct Fd_set FDS;
struct Timeval timeout={0,1};
Fd_zero (&FDS);
Fd_set (SOCK_SEV,&FDS);
if (select (maxfdp,&fds,null,null,&timeout))
{
cout<< "new Connection" <<endl;
Sock_client = Accept (Sock_sev, (SOCKADDR *) &addr_client, &naddrlen);
if (Invalid_socket = = sock_client)
{
std::cout << "Failed to accept." << Std::endl;
Continue;
}
socketclient[i].socketconn=sock_client;
Std::cout << "Connection from" << Inet_ntoa (addr_client.sin_addr) << Std::endl;
Socketclient[i]. clientid=i;
Socketclient[i].socketthread=createthread (Null,0,threadread,&socketclient[i],0,socketclient[i]. ThreadID);
i++;
}