Go Back   FlashFXP Forums > >

Programming Need help with C/C++/Delphi? Ask here and make us all laugh.

Closed Thread
 
Thread Tools Rate Thread Display Modes
Old 09-12-2004, 05:36 PM   #1
mr_F_2
Senior Member
 
Join Date: Jan 2004
Posts: 203
Default making a winsock listen loop in C

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
mr_F_2 is offline  
Old 09-12-2004, 08:01 PM   #2
Syrinx
Junior Member
 
Join Date: Jun 2004
Posts: 25
Default Re: making a winsock listen loop in C

Quote:
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.
Syrinx is offline  
Old 09-12-2004, 09:46 PM   #3
Mouton
Posse Member
Ultimate Scripter
ioFTPD Administrator
 
Join Date: Dec 2002
Posts: 1,956
Default

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.
Mouton is offline  
Old 09-12-2004, 10:33 PM   #4
mr_F_2
Senior Member
 
Join Date: Jan 2004
Posts: 203
Thumbs up

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
mr_F_2 is offline  
Old 09-13-2004, 01:10 PM   #5
darkone
Disabled
FlashFXP Registered User
ioFTPD Administrator
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default

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.
darkone is offline  
Closed Thread

Tags
close, conection, cpu, loop, routines;

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:54 PM.

Parts of this site powered by vBulletin Mods & Addons from DragonByte Technologies Ltd. (Details)