Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Networking Question

Status
Not open for further replies.

Galarun

Programmer
Jun 28, 2005
24
0
0
US
'Lo all,

I'm programming a card game and am trying to add networking capabilities so you can play against somebody on another computer connected to the network. My question is basically on threading, but I'm new to both threading and networking so any advice would be greatly appreciated.

Basically, before the game starts each player must click on the deck to signal to the other player that they are ready to start the game. Whoever clicks the deck first will have a messaged displayed to them that they must wait for their opponent to click on the deck. Once the opponent clicks on the deck both players will be dealt their cards.

How my code works in this situation is that when a player clicks on a deck it sends a signal to the other player that they are ready. Whoever clicks the deck will call an event that determines whether there is a confirmation to play already. If there is, then the player sends a reply to signify that he is ready and then calls the deal function. If there isn't a confirmation, then a message is displayed telling the user to wait for the opponent and then a new thread is started that waits for a response. Once the response is recieved then it breaks the loop and calls the deal function.

Here's my problem: After the cards are dealt each player must throw two cards into a hand in the middle (this is cribbage, btw). When the deal function is called it deals the cards and then creates a new thread that waits to see when the opponent chooses and throws their cards. My problem here is that when the second person clicks the deck to signify that he is ready to play, the second person is dealt their cards (because no new thread is created) but the first player doesn't do anything (no cards are dealt). My guess to why this is happening is that when the first player receives the comfirmation they call the deal function with creates another thread that listens to see when the opponent chooses their two cards to throw, and I'm not sure what happens when you create a new thread within a created thread.

One solution I found was to join the created thread that listens for the comfirmation after it is started so that it joins the old thread when it is finished and THEN calls the deal function, but when I tried this the message to wait for the opponent wasn't displayed because it never left the function until the opponent agreed to play. Like I said before I'm new to thread programming so I'm not sure why this is happening. Here's my code for the relevant sections, and again any help is appreciated!!!

When somebody clicks on the deck it calls this:

IFormatter formatter = new BinaryFormatter();

if (serverStream.DataAvailable)
{
serverStream.Flush(); formatter.Serialize(serverStream, "ready");
StartRound();
}
else
{
serverStream.Flush(); formatter.Serialize(serverStream, ready");
serverStream.Flush();
lblContinue.Text = "Waiting for Opponent";

Thread t = new Thread(new ThreadStart(WaitForReply));
t.Start();
}

public void WaitForReply()
{
IFormatter formatter = new BinaryFormatter();
string reply;

while (true)
{
if (serverStream.DataAvailable)
{
reply = formatter.Deserialize(serverStream).ToString();
serverStream.Flush();
break;
}
}
StartRound();
}

public void StartRound()
{
lblContinue.Visible = false;
lblContinue.Text = "Click Deck to Continue";
DealHands();
PrePeg = true;
Player1.ThrowCrib.Visible = true;

Thread t = new Thread(new ThreadStart(ListenToClient));
t.Start();
}
 
Fixed it.

After learning a bit more about threads I realized using delegates and events would work perfectly. I just ran an event after the thread function ended which updated the UI.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top