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;
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)
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) + "#";
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();
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();
}