Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
okay, at first i was working with non-blocking sockets, but then when i was trying to catch individual events from multiple users, it quickly became cluttered and innaccurate. So much so, that i think even multithreading is a better alternative, just using mutexes. But i've got a problem. in each thread for each connection, it is supposed to block at recv and wait indefinately, but then, let's say that i wish to shutdown the server, and end the thread mid recv? is there anyway to force recv to return immediately? does setting the socket to non-blocking mode from another thread, mid recv, maybe cause it to return or does it HAVE to be set before recv is called?
Posted

1 solution

Use select() to see if you have a packet waiting before you call recv().

If no packet is waiting, then don't call recv.

You might call Sleep(1) instead.
 
Share this answer
 
Comments
FatalCatharsis 18-Nov-11 13:33pm    
Perfect, that's exactly what i'm looking for. Thanks a bunch!
FatalCatharsis 18-Nov-11 13:39pm    
actually, wait no. i'm not able to check right now, but wouldn't that require a for loop that would loop over and over, and suck up my cpu? cause that's one of the problems i had with non-blocking sockets. it would constantly cycle and see if anything was available, and would always use 100% cpu :\ .
JackDingler 18-Nov-11 13:53pm    
That's why you put in a sleep statement or some other sort of wait call.

Sleep() suspends the thread for the specified number of milliseconds. During this time, your thread is not using the CPU.
FatalCatharsis 18-Nov-11 14:10pm    
wow, knowing that would have solved a TON of problems i've had in the past. Even more thanks to you :P

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900