Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In my curses based game, I decided to add multiplayer functionality over net, in addition to one-keyboard mulitiplayer game. I use switch loop and in case it's 1st user's buttons, user 1 is acting appropriately and second otherwise, without blocking each other's movements i.e not waiting for opponents move. Now I read socket tutorials, and use server looking like this:

wait for 1st user to connect wait for 2nd user to connect

loop: read from 1st write to 2nd read from 2nd write to 1st

Now problem is obvious, players would block each others movements, how this network multiplayer problems solved in games design?

share|improve this question
    
You can use non-blocking sockets. –  Luis Estrada Aug 9 '12 at 18:24
add comment

1 Answer

Basically you wouldn't write a server like that. Those tutorials are showing you the basics of 2 reading and writing and assume 2 trusted processes that are guaranteed to cooperate.

Instead of waiting for a message from one client before the code continues to send a message out to another client, the server waits for messages from all clients simultaneously. When one arrives, it will check its business logic to see if the message came from the client it expected, and if so, it will process it accordingly. So instead of a loop which reads from each side in turn, you read from whoever sent something first, respond accordingly, and repeat.

To wait for multiple clients you usually use some variation on the select() function to tell you which client has data ready for you to read it. Or better still, you use a library that wraps that (or a similar alternative) in some way.

There's some explanation of the select() function here: http://beej.us/guide/bgnet/output/html/multipage/advanced.html

The code there is in C, but you'd use something very similar in most languages. If you use a 3rd party library, usually they handle this part for you, and call a callback that you provide when the message comes in.

share|improve this answer
    
As a note in case the asker is on windows and using the full winsock API, waitformultipleobjects is the "select variant" to look for. –  Sean Middleditch Aug 9 '12 at 21:58
3  
'select' is available under winsock2 (which has been available since Win2k). msdn.microsoft.com/en-us/library/windows/desktop/… –  Trevor Powell Aug 9 '12 at 23:56
1  
Even though select is available, MsgWaitForMultipleObjectsEx is the more powerful API. It allows you to capture a far wider variety of event sources (sockets, Windows messages, Windows events, Asynchronous file I/O). –  MSalters Aug 10 '12 at 8:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.