Jump to content
  • Advertisement
Sign in to follow this  
nightech

Turn based multiplayer TCP question

This topic is 3831 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

I'm currently building a client/server turn based multiplayer board game, my current message passing implementation works, but right now it needs to know whether it should be reading or writing. I use the Read and Write functions for the networkstream, so basically, when it is a specific players turn, the server is in read mode for that player, and all other players are in read mode. Only the player whose turn it is is writing to the stream. This makes it impossible to implement chat functionality on the same networkstream. I would like to implement an asyncronous read and write to the networkstream, more explicitly, i've been looking into the BeginRead and BeginWrite functions, i've tried to find some detailed examples, but i've not come across anything that answers my questions yet. Basically, what i understand is that BeginRead and BeginWrite run the read and write functions in their own threads. I'm making the implementation in VB.Net 2008 if it makes any difference. Here are my questions. 1. Does this mean that after using the BeginRead command, execution will continue in the main process and allow me to also use a BeginWrite command that will allow concurrent bidirectional communication on the networkstream? 2. When the associated call back function is called, does the BeginRead and BeginWrite continue to process additional data or does it need to be called once again? 3. Since the read and write commands are part of a main loop, i'm using the thread sleep function to stop the loop from just constantly running and checking. Is there a way i can just pause the loop, pending disconnection of either the read or write (or both) connections? 4. Is there some other option i should be using? Thanks in advance for your input.

Share this post


Link to post
Share on other sites
Advertisement
I'll assume you have TCPListener getting all of your connections and you add them to a connected list of sockets.

The BeginSend and BeginReceive stuff on the socket is asynchronous meaning it doesn't block the thread it's running in. It uses a callback to report when it's done.

One way to handle a game like this is to tell a player when it's his/her turn then in the receive callback for players filter the data for that player. Allow them to take their turns then when a timer runs out or they send a certain message move onto the next player.

So in the receive callback you have something like

private void ReceiveCallback(Asyn... ar)
{
ReceiveStateObject stateObject = (ReceiveStateObject)ar;
if(ThisPlayersTurn)
{
//perform player specific stuff
}
//perform chat and everything else
}

2) BeginSend is send and forget. Rarely do you care about the callback. The BeginReceive you have to call again at the end of the callback so that it continues to receive data. It's very important you catch the exceptions for these and handle them appropriately!

Using asynchronous stuff your main thread might not even be needed except to keep a timer if you want to switch players who don't respond. You can handle the switching players when players end their turn and stuff and never have a while loop :P

For client disconnections you can expect to get a socket exception somewhere. Intellisense will tell you what exceptions can be thrown. Most are fatal and require the client to be disconnected so you just remove it from the list of players and handle everything accordingly.

Share this post


Link to post
Share on other sites
Thanks Sirisian, that is possibly the best answer i could have hoped for. I'll probably update my implementation over the next few days and see how i go.

You were spot on with the TCPListener, i should have specified that.

Thanks again :)


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!