Code:
// Make the socket listen
nRet = listen(listeningSocket, 1);
if (nRet == SOCKET_ERROR) {
printf("Error at listen() :: listeningSocket\n");
WSACleanup();
return 0;
}
while(1) {
// Wait for a client
theSocket = accept(listeningSocket,
NULL, // Address of a sockaddr structure (see below)
NULL); // Address of a variable containing the size of sockaddr
if (theSocket == INVALID_SOCKET) {
printf("Error at accept() :: listeningSocket\n");
WSACleanup();
return 0;
}
if(_beginthread( read_socket, 0, NULL )==-1)
{
printf("Error in creating thread read_client\n");
return 1;
}
}
accept() is not asynchronous... Meaning it won't execute the next line until a client connects... and while it waits, it won't use any cpu.
For asynchronous daemon, I don't know. I'm pretty sure a windows message would be sent on connect, which needs to be attached to the function to be called on client connections. But what do u do in your main thread, I don't know. Google probably has more than enough C asynchronous socket tutorials.