forms and tcp

Im not sure how to go about this but i want to connect using Form1 to a tcp connection then when i open form2 I wish to be able to type a string in a textbox say "Help!" and then click on a button to send this throught the connection I make in Form1.

I have coded the following but its not working, I have missed something but I dont know what and how to fix this! Please help!

In my first form "Form1" I connect using....

try

{

myclient = new TcpClient(ipaddress.Text, 1001);

}

catch

{

MessageBox.Show("TcpClient Failed");

return;

}

networkStream = myclient.GetStream();

streamReader = new StreamReader(networkStream);

streamWriter = new StreamWriter(networkStream);

I then open a new form to send a message like so....

private void button_Click(object sender, EventArgs e)

{

Form2 f2 = new Form2();

f2.Show();

}

I then use a textbox "t1" where I enter the message and a button "b1" to send the message. On clicking the button "b1" the following code is used....

private void b1_Click(object sender, EventArgs e)

{

if (t1.Text == "")

{

MessageBox.Show("Please enter something in the textbox");

t1.Focus();

return;

}

try

{

Form1.streamWriter.WriteLine("1,msg," + t1.Text);

Form1.streamWriter.Flush();

}

catch (Exception ee)

{

Console.WriteLine("Exception reading from Server:" + ee.ToString());

}

}

This seems fine to me, but im not sure when using different forms, why doesnt it work any ideas

Thanks

KP





Answer this question

forms and tcp

  • forms and tcp