Making a Client Wait for Server Response

I'm trying to write a "Basic" echo like program, the Client sends a String to the server, the server does some stuff, and sends a string back to the client. I'm Having trouble figuring out how to have the Client "Wait" untill a response is sent before reading the lines in. so far I've been able to make some nice infinite loops, and other cases it just sits at "ReadLine" from the buffer reader.

Here is some snips, i've tried to remove un-neccissary code...

public partial class Main : Form{

public static string _host = "127.0.0.1"; //host (ip)

public static string _port = "7010"; //port to connection on

public static string _name = "Name";

public TcpClient Client;

public StreamReader sr;

public StreamWriter sw;

string nextline = "\r\n"; //I dont get it, C# wont accept the special character as a Char, only string... Throws errors, so its a string.

public Main(){

InitializeComponent();

Client = new TcpClient();

this.KeyPress += new KeyPressEventHandler(KeyPressed);

}

public void connectToolStripMenuItem_Click(object sender, EventArgs e){

//connect to the server

//We should try to lookup the server here, incase its a domain name...

try{

Client.Connect(_host, Convert.ToInt32(_port));

Stream s = Client.GetStream();

sr = new StreamReader(s);

sw = new StreamWriter(s);

sw.AutoFlush = true;

//moutput.Text += sr.ReadLine() +nextline; //Read MoTD from the server

}

catch (Exception ex){

moutput.Text += _host + ":" + _port + " - " + ex.Message + nextline;

}

}

public void Send_Click(object sender, EventArgs e){

try{

sw.WriteLine(minput.Text);

minput.Text = "";

}

catch (Exception ex){

moutput.Text += _host + ":" + _port + " - " + ex.Message + nextline;

}

From here i have no idea how to have the server "Wait" for a reply otherwise It just infinte loops or waits at "sr.ReadLine()". Any Help would be apperciated. All I want it to do is Write out what it gets from the server into the "moutput" box.




Answer this question

Making a Client Wait for Server Response

  • Montty

    So what exactly the server is sending
    Is it merely copying the data back to client
    If not is it sending \r\n at the end of line

    What would be helpful is to get a network minitor trace.
    you can use ethereal. You can also obtain a system.net trace
    look at http://blogs.msdn.com/dgorti for details

  • Fonzy

    What does your server code look like You have only given us half of the equation...

  • Making a Client Wait for Server Response