looking for Socket.select with maximum time or other solution
Regan Heath
regan at netmail.co.nz
Mon Oct 8 01:20:31 PDT 2007
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
More information about the Digitalmars-d-learn
mailing list