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 HobbsRank #107 · 20.5K reputationPosted Dec 28, 2009, 10:01 AM EST
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 HobbsRank #107 · 20.5K reputationPosted Dec 27, 2009, 4:45 PM EST
I think that C# is sloppy; we have to both flush and close, whereas in C++ the destructor would do the equivalent automatically.
yokzuRank #5,421 · 300 reputationPosted Dec 28, 2009, 2:59 AM EST
yokzuRank #5,421 · 300 reputationPosted Dec 28, 2009, 2:08 PM EST
Sam HobbsRank #107 · 20.5K reputationPosted Dec 29, 2009, 6:50 PM EST