Hi.
I am trying to some socket programs. Below, server side program's codes. Its getting the messages from client with no problem. I want to count this messages length and sending again to client. But it doesnt work. Where am I mistaken?
public NetworkStream str;
public StreamReader str_rdr;
public StreamWriter str_wrt;
public Socket clientsoket;
public void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
Thread thread_listener = new Thread(new ThreadStart(listen));
thread_listener.Start();
}
public void listen()
{
TcpListener listener = new TcpListener(30000);
while (true)
{
listener.Start();
Socket clientsocket = listener.AcceptSocket();
NetworkStream str = new NetworkStream(clientsoket);
StreamReader str_rdr = new StreamReader(str);
string var1 = str_rdr.ReadLine();
textBox1.Text = var1; // ----> In here, I am seeing client's message.
StreamWriter str_wrt = new StreamWriter(str);
str_wrt.WriteLine(textBox1.Text.Length);
str_wrt.Flush();
}
}
Loading
Sam HobbsPosted Dec 28, 2009, 3:01 PM
Then create a method:
Then instead of:
Do:
Also instead of:
Do:
That works for me! In my version of your code, I re-arranged other things but I don't know if that affected the results. So if you make the above changes and it still does not work, then we can try some more changes.
Sam HobbsPosted Dec 29, 2009, 11:50 PM
yokzuPosted Dec 28, 2009, 7:08 PM
yokzuPosted Dec 28, 2009, 7:59 AM
Sam HobbsPosted Dec 27, 2009, 9:45 PM
I think that C# is sloppy; we have to both flush and close, whereas in C++ the destructor would do the equivalent automatically.