Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
I m working on a Multiplayer Poker card game. I have declared a static variable as
public static class GameInfo
  {
      public static string GameID;
  }

to use its in multiple winforms. Similar to the the above declaration i have another varibale declared as
public static class LoginInfo
  {
      public static string UserID;
  }


I have to send a game code to the server. when i use the following statement the client send the whole value of GameInfo.GameID at once.
GameInfo.GameID = "$POINT$" + LoginInfo.UserID + "#";

But the problem is that i need to select some text from listbox to send to the server. What i want to do is
GameInfo.GameID = "$POINT$" + listBox1.SelectedItem.ToString().Substring(16)+"#";

But when i use the server gets the value in parts. First it receives $POINT$+listbox selected text
and after that it send # and the further value.
The code i used to send the value to the server is
outStream = System.Text.Encoding.ASCII.GetBytes(GameInfo.GameID + LoginInfo.UserID + "#" + point);
serverStream.Write(outStream, 0, outStream.Length);

I tried to check the value of GameInfo.GameID (GameInfo.GameID = "$POINT$" + listBox1.SelectedItem.ToString().Substring(16)+"#";) by printing it in the same winform. But it does not append "#" chracter in the last. But when i send it to server it receives it as second part.
With GameInfo.GameID = "$POINT$" + LoginInfo.UserID + "#"; everthing works fine.
The problem arise when i use listbox1.SelectedItem.ToString();

private void button2_Click(object sender, EventArgs e)
       {
           if (listBox1.SelectedItem != null)
           {
               GameInfo.GameID = "$POINT$"+ listBox1.SelectedItem.ToString().Substring(16)+"#";
               //GameInfo.GameID = "$POINT$" + LoginInfo.UserID + "#";
               //this.Close();
               richTextBox1.Text = richTextBox1.Text + Environment.NewLine + GameInfo.GameID;
               Form4 form4 = new Form4();
               form4.Show();
           }
           else
           {
               label3.Text = "Please Select a Game";
           }
       }

This is the code i used to print the value of GameInfo.GameID. But is does not appends # character.
Ok say i m login by name xyz so LoginInfo.UserID has xyz the complete string would be

$POINT$xyz#

Now with ListBox i have an item "Game Created By xyz" so i extracted xyz by using substring function. But with this
GameInfo.GameID = "$POINT$"+ listBox1.SelectedItem.ToString().Substring(16)+"#";
# charcter is not appended.
The string i get is $POINT$xyz
Posted
Updated 26-Jan-14 2:45am
v8
Comments
Richard MacCutchan 26-Jan-14 7:24am    
You need to correct your receive code in the server to ensure it keeps reading until all parts of the message are received. Look for TCP samples and you will get some code that will help you.
Member 10016481 26-Jan-14 7:41am    
Yeah i know that solution But my requirement is to send the whole sting at once and GameInfo.GameID = "$POINT$" + LoginInfo.UserID + "#"; this works fine. what i need is to send value selected from listbox. And
GameInfo.GameID = "$POINT$" + listBox1.SelectedItem.ToString().Substring(16)+"#";
sends the value in two parts.
Richard MacCutchan 26-Jan-14 8:12am    
You need to understand that sending the string in one go does not guarantee that it will be received in the same way. Your receiving code must be written in such a way that it can read multi-part messages.
Member 10016481 26-Jan-14 8:14am    
But with GameInfo.GameID = "$POINT$" + LoginInfo.UserID + "#"; everything works fine.
Richard MacCutchan 26-Jan-14 8:22am    
Maybe it's a shorter message, maybe there was less traffic at that moment, maybe ...
This is the way TCP works so you need to code for it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900