PDA

View Full Version : making a winsock listen loop in C


mr_F_2
09-12-2004, 05:36 PM
i'm wondering about this, for the purpose of server applications. every loop i've ever made to listen for connections utilizes close to 100% cpu because of constant conection checks.

basically i always end up with something like this (in psuedocode)

main {

pre configuration routines;
open socket for listening;

loop {

if (conection) {
dosomething
}

close socket;
post routines;
return;

}

how do i do this better, or how do i make it utilize less cpu resourcees? i'm good with C but am not familiar with the available windows API, so i don't really know about controlling cpu utilization via setting a variable or whatever.. please help

Syrinx
09-12-2004, 08:01 PM
Originally posted by mr_F
i'm wondering about this, for the purpose of server applications. every loop i've ever made to listen for connections utilizes close to 100% cpu because of constant conection checks.

basically i always end up with something like this (in psuedocode)

main {

pre configuration routines;
open socket for listening;

loop {

if (conection) {
dosomething
}

close socket;
post routines;
return;

}

how do i do this better, or how do i make it utilize less cpu resourcees? i'm good with C but am not familiar with the available windows API, so i don't really know about controlling cpu utilization via setting a variable or whatever.. please help

This model looks fine since all multi-threaded with blocking socket server application mostly in that form.I don't know what you mean by "constant conection checks".I gues your application must be something wrong in the code.

Mouton
09-12-2004, 09:46 PM
// 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.

mr_F_2
09-12-2004, 10:33 PM
hmm, that's strange, so my guess is that somehow i was using a method that didn't hang waiting for a connection, rather it kept checking repeatedly

thanks for your help guys i'm going to put that to work :)

darkone
09-13-2004, 01:10 PM
Sounds like you have set socket to non-blocking mode using WSAEventSelect(), WSAAsyncSelect() or WSAIoctl(). Btw. WSAAsyncSelect() is the easiest non-blocking api to use, and it's not that terrible performer either.