Jump to content
  • Advertisement
Sign in to follow this  
retroworld

non-blocking and non-threaded??

This topic is 2800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!