Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBacterius

Posted 31 August 2013 - 08:31 PM

The basic Winsock sockets are blocking, both on accept(), recv(), and to a certain extent send(). That means that you can't do more than one thing at a time with a single thread. There are multiple solutions:

- have a master thread which contains the server socket which listens for incoming connections and accept()'s them, and spawn a new thread for each client that connects. The advantages is that it is straightforward and gets the job done, but the downside is that it does not scale with more than a few clients.

- use asynchronous, event driven sockets (available in Winsock with no particular library required) which basically fire an event every time something of interest happens on the network, very comfortable to use as you can put those events into a queue and pick them up in your game loop, without needing threads, and letting Winsock do all the heavy lifting for you (you'll probably need a little network class to parse those raw events into something your game can use, though). It is very flexible as well.

- use IOCP, which is an advanced technique which is very efficient and scales extremely well, unfortunately it is a pain to implement and you will probably want to use a preexisting library. However, IOCP is not really meant for stuff like games where people stay connected for a long time and send lots of data, though it can be made to work - it is more for web services which need to handle up to thousands of simultaneous (lightweight) requests, things like that.

 

In your case, I would suggest giving event-driven networking a shot. It feels alien if you've only ever done blocking networking, but it can be liberating. Don't just into your game with it, though, get comfortable with it by writing prototypes and samples before.


#1Bacterius

Posted 31 August 2013 - 08:30 PM

The basic Winsock sockets are blocking, both on accept(), recv(), and to a certain measure send(). That means that you can't do more than one thing at a time with a single thread. There are multiple solutions:

- have a master thread which contains the server socket which listens for incoming connections and accept()'s them, and spawn a new thread for each client that connects. The advantages is that it is straightforward and gets the job done, but the downside is that it does not scale with more than a few clients.

- use asynchronous, event driven sockets (available in Winsock with no particular library required) which basically fire an event every time something of interest happens on the network, very comfortable to use as you can put those events into a queue and pick them up in your game loop, without needing threads, and letting Winsock do all the heavy lifting for you (you'll probably need a little network class to parse those raw events into something your game can use, though). It is very flexible as well.

- use IOCP, which is an advanced technique which is very efficient and scales extremely well, unfortunately it is a pain to implement and you will probably want to use a preexisting library. However, IOCP is not really meant for stuff like games where people stay connected for a long time and send lots of data, though it can be made to work - it is more for web services which need to handle up to thousands of simultaneous (lightweight) requests, things like that.

 

In your case, I would suggest giving event-driven networking a shot. It feels alien if you've only ever done blocking networking, but it can be liberating. Don't just into your game with it, though, get comfortable with it by writing prototypes and samples before.


PARTNERS