www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - looking for Socket.select with maximum time or other solution

reply Charma <Motoko_Kusanagi web.de> writes:
Hello everyone!
I am currently working on a server which many clients can connect to and 
i am using socket.select with my socketset which all clients are in. 
This far everything works very fine. But i would prefer my server-loop 
to continue e.g. at least 10 times every second to work on other things 
even when nothing is send from clients.

Is there anything i can do to force socket.select to stop waiting after 
a given time even if all sockets will stay as marked with nothing to 
receive?

Maybe someone has an even better/other/different idea?!?

Also i would like to know: Is there any way i can cancel socket.select 
while it is waiting? I mean, i would like to be able to quit my server 
clean without needing to connect with a client again and so on...

Thanks for any suggestions and help!!
Sorry about my engrish!!

Charma
Oct 07 2007
next sibling parent Nathan Reed <nathaniel.reed gmail.com> writes:
Charma wrote:
 Hello everyone!
 I am currently working on a server which many clients can connect to and 
 i am using socket.select with my socketset which all clients are in. 
 This far everything works very fine. But i would prefer my server-loop 
 to continue e.g. at least 10 times every second to work on other things 
 even when nothing is send from clients.
I'd use a separate thread for that.
 Also i would like to know: Is there any way i can cancel socket.select 
 while it is waiting? I mean, i would like to be able to quit my server 
 clean without needing to connect with a client again and so on...
How about, from another thread, killing the thread that is running select? Yeah, a little bit clumsy, but at least you don't have to kill the process from the console. Thanks, Nathan Reed
Oct 07 2007
prev sibling parent reply Regan Heath <regan netmail.co.nz> writes:
Charma wrote:
 Hello everyone!
 I am currently working on a server which many clients can connect to and 
 i am using socket.select with my socketset which all clients are in. 
 This far everything works very fine. But i would prefer my server-loop 
 to continue e.g. at least 10 times every second to work on other things 
 even when nothing is send from clients.
 
 Is there anything i can do to force socket.select to stop waiting after 
 a given time even if all sockets will stay as marked with nothing to 
 receive?
This version of select takes a timeout parameter of type timeval: static int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, timeval* tv); you will need to reload the sockets into the set before every call.
 Maybe someone has an even better/other/different idea?!?
 
 Also i would like to know: Is there any way i can cancel socket.select 
 while it is waiting? 
I believe you could interrupt it from another thread but I don't think that it is necessary, simply use a short timeout period and make the main loop check a termination variable eg. while(!timeToStop) { select...with short timeout. } Of course, you need some method of setting timeToStop, perhaps you check for keyboard input in the main loop, perhaps a client connection processes a command like "shutdown"... You can use a ticker/counter variable in the main loop to restrict how often certain tasks are carried out, eg. ticker=0 while(!timeToStop) { select...with timeout of 100ms (10x/sec) if (ticker%5 == 0) { //2x/sec //put stuff in here that only happens 2x/sec } ..etc.. } It may be a cleaner design, and better in the multi-cpu enviroment we have today to create a thread for each task, where the thread simply sleeps between tasks.
 I mean, i would like to be able to quit my server
 clean without needing to connect with a client again and so on...
In that case perhaps a check for keyboard input is what you want. Regan
Oct 08 2007
parent Charma <Motoko_Kusanagi web.de> writes:
Regan Heath schrieb:
 Charma wrote:
 Hello everyone!
 I am currently working on a server which many clients can connect to 
 and i am using socket.select with my socketset which all clients are 
 in. This far everything works very fine. But i would prefer my 
 server-loop to continue e.g. at least 10 times every second to work on 
 other things even when nothing is send from clients.

 Is there anything i can do to force socket.select to stop waiting 
 after a given time even if all sockets will stay as marked with 
 nothing to receive?
This version of select takes a timeout parameter of type timeval: static int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, timeval* tv); you will need to reload the sockets into the set before every call.
 Maybe someone has an even better/other/different idea?!?

 Also i would like to know: Is there any way i can cancel socket.select 
 while it is waiting? 
I believe you could interrupt it from another thread but I don't think that it is necessary, simply use a short timeout period and make the main loop check a termination variable eg. while(!timeToStop) { select...with short timeout. } Of course, you need some method of setting timeToStop, perhaps you check for keyboard input in the main loop, perhaps a client connection processes a command like "shutdown"... You can use a ticker/counter variable in the main loop to restrict how often certain tasks are carried out, eg. ticker=0 while(!timeToStop) { select...with timeout of 100ms (10x/sec) if (ticker%5 == 0) { //2x/sec //put stuff in here that only happens 2x/sec } ..etc.. } It may be a cleaner design, and better in the multi-cpu enviroment we have today to create a thread for each task, where the thread simply sleeps between tasks. > I mean, i would like to be able to quit my server
 clean without needing to connect with a client again and so on...
In that case perhaps a check for keyboard input is what you want. Regan
Regan, As usual, you had exactly what i was looking for! For some reason, i don't know by myself, i was always overlooking that there exists a parameter for that... Thanks a lot! This solves my problem :) I want to avoid using threads to keep my server as fast and efficient as possible, so i will try to come up with something for a completly seperate connection which will be handled different from the other clients... Greetings, Charma
Oct 08 2007