Sign in to follow this  
retroworld

non-blocking and non-threaded??

Recommended Posts

Okay, I am not that experienced yet to use threads and blocking sockets.

how would you advice me to make a server that is non-blocking and non-threaded?

I want to use select for my socket clients but how would I go about accepting new clients?

Share this post


Link to post
Share on other sites
more something like:
for(int i = 0; i < select(); ++i)
{
new_connection = accept();
hand_off_or_do_whatever(new_connection);
}


accept will block forever or until a connection arrives. Which is not what you want.

select waits on a socket until a connection arrives or it times out, and returns the number of waiting connections, which may be zero or more than one. Use a zero timeout if you are not willing to block at all, or use some small value.

Share this post


Link to post
Share on other sites
Quote:
Original post by retroworld
do you mean line client = select(accept())?


No. I explicitly meant treat the listening (accepting) socket as an input socket, but instead of calling recv() when it returns ready, call accept() on it.

Typical select() main loop:

forever() {
put all sockets into read set
put all sockets with queued output data into write set
call select(read set, write set, timeout)
foreach (socket in write set) {
send() on the socket
dequeue the sent data
}
foreach (socket in read set) {
if (socket == listening socket) {
accept()
set up new client
}
else {
recv() on the socket
}
}
foreach (socket with incoming data) {
de-frame/dispatch protocol data
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this