Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to append # character on button2_Click event. If i try to print value of GameInfo.GameID
in the same form the # character is not appended. But in the next form if i send its value to the server the # character is sent to the server as second part of the string sent.
Can anyone explain the problem with my code? I

public partial class Form3 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);
       
        string readData = null;
        string USERNAME = null;
        string returndata = null;
        int LENGTH = 0;
        //TO CHECK IF FORM IS ALREADY SHOWN OR NOT
        bool CHECKINSTANCE = false;
        public Form3()
        {
            InitializeComponent();
        }
        private void Form3_Load(object sender, EventArgs e)
        {
      
            msg();
            clientSocket.Connect("127.0.0.1", 8888);
            serverStream = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("$DU" + LoginInfo.UserID + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            Thread ctThread = new Thread(getMessage);
            ctThread.Start();


        }
        private void getMessage()
        {
            string trim = null;
            try
            {
                while (true)
                {
                    readData = null;
                    returndata = null;
                    USERNAME = null;

                    LENGTH = 0;
                    int CUT = 0;
                    serverStream = clientSocket.GetStream();
                    int buffSize = 0;
                    byte[] inStream = new byte[10025];
                    buffSize = clientSocket.ReceiveBufferSize;
                    serverStream.Read(inStream, 0, buffSize);
                    returndata = System.Text.Encoding.ASCII.GetString(inStream);
                    serverStream.Flush();
                    readData = returndata;

                    trim = readData.Substring(0, 6);
                    if (trim == "$CHAT$")
                    {
                        readData = readData.Remove(0, 6);

                        for (int i = 0; i < readData.Length; i++)
                        {

                            if (readData[i] == '&')
                            {
                                USERNAME = readData.Substring(0, readData.IndexOf("&"));
                                CUT = i + 1;
                                break;
                            }
                        }
                        if (CUT != 0)
                        {
                            readData = readData.Substring(CUT);
                        }
                        if (USERNAME != null)
                        {
                            for (int i = 0; i < USERNAME.Length; i++) ;
                            {
                                LENGTH = LENGTH + 1;
                            }
                        }
                        msg();
                    }
                    else if (trim == "$GAME$")
                    {
                        gamemsg();
                    }

                }
            }
            catch
            {
            }
        }
        private void msg()
        {
            string temp=null;
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(msg));
            else
            {   
               
                richTextBox1.ReadOnly = false;
                if (USERNAME == null)
                {
                    richTextBox1.Text = richTextBox1.Text + Environment.NewLine + readData;
          
                }
                else if (USERNAME == LoginInfo.UserID)
                {  
                    richTextBox1.Text = richTextBox1.Text + Environment.NewLine + "Me >> " + readData;
                          }
                else
                {
                    richTextBox1.Text = richTextBox1.Text + Environment.NewLine + USERNAME + " says>> " + readData;
          
                }

            }

        }

        private void gamemsg()
        {
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(gamemsg));
            else
            {
                bool match = false;
                  readData = readData.Remove(0, 6);
                 USERNAME = readData;

                 USERNAME = readData.Substring(0, readData.IndexOf("&") - 1);
                 for (int i = 0; i < listBox1.Items.Count; i++)
                 {
                     if (listBox1.Items[i].ToString() == "Game Created by " + USERNAME)
                         match = true;
                 }
                 if (match == false)//to save from listing duplicate games
                     listBox1.Items.Add("Game Created by " + USERNAME);
               
            }
        }

        }

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

}
    
    }


Here is the code i used in another winform to send it to the server

private void Form4_Load(object sender, EventArgs e)
        {
            string readData = null;
            int buffSize = 0;
            byte[] outStream = null;
            byte[] inStream = new byte[10025];
            int point = 0;
           
       
            clientSocket.Connect("127.0.0.1", 8888);
            serverStream = clientSocket.GetStream();
            // send code to get the card
            outStream = System.Text.Encoding.ASCII.GetBytes("$CARD$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
            
            buffSize = clientSocket.ReceiveBufferSize;
            serverStream.Read(inStream, 0, buffSize);
            readData = System.Text.Encoding.ASCII.GetString(inStream);
            serverStream.Flush();
            assigncard(readData);
            point=assignpoints(Code1,Code2,Code3,Code4,Code5);
            Flush(Code1, Code2, Code3, Code4, Code5);

            
            outStream = System.Text.Encoding.ASCII.GetBytes(GameInfo.GameID + LoginInfo.UserID + "#" + point);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
             
            Thread pointThread = new Thread(getPoints);
            pointThread.Start();
        }
Posted
Updated 28-Jan-14 3:24am
v3
Comments
bowlturner 28-Jan-14 9:04am    
is the text box big enough to display the whole string?
Member 10016481 28-Jan-14 9:22am    
Yeah its a multiline textBox
Richard MacCutchan 28-Jan-14 9:20am    
You can easily check the contents of GameInfo.GameID with your debugger, or use some logging method to write it to a file. However, the most important part(s) of the code is where you send this message to the server, also where the server reads it, and you still have not shown that.
Member 10016481 28-Jan-14 9:25am    
Sir i have included the code i used to send it to the server.
Richard MacCutchan 28-Jan-14 9:35am    
You are not checking that you have read the complete message in your read process. See http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.read(v=vs.110).aspx for how it should be done.

1 solution

You should use the return value from the Read method to build your string, based on the number of characters actually returned. Something like:
C#
int buffSize = clientSocket.ReceiveBufferSize;
byte[] inStream = new byte[buffSize];
int bytesRead = serverStream.Read(inStream, 0, buffSize);
returndata = System.Text.Encoding.ASCII.GetString(inStream, 0, bytesRead);
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 29-Jan-14 8:52am    
5
Richard MacCutchan 29-Jan-14 9:03am    
Thank you; it took me while to figure it out, but now it seems so obvious ... :)
Karthik_Mahalingam 29-Jan-14 9:07am    
yes,Richard
i saw the conversation..
Good job..

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