0
TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse(HOST_IP), HOST_PORT);
Stream stream = client.GetStream();
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true;
while (true) {
  string line;
  while((line = reader.ReadLine())!=null) {
    Console.WriteLine(line);
  }
  writer.WriteLine(Console.ReadLine());
}

My server sends two strings to the client:

var stream = new NetworkStream(soc);
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true
writer.WriteLine("Welcome: ");
writer.WriteLine("Bla...: ");
reader.ReadLine();

My client get will show

Welcome
Bla..

But it doesn't run the command writer.WriteLine(Console.ReadLine());

NamPNQ
  • 379
  • 4
  • 13
  • Do you do nothing else on the server side? How is the client side meant to know that, in five minutes time, the server might make another `writer.WriteLine()` call which the client needs to read in its `while` loop. – Damien_The_Unbeliever Nov 18 '12 at 15:38
  • Sorry, in my server has `reader.ReadLine()` after last write – NamPNQ Nov 18 '12 at 15:48
  • Yes, but how is the client meant to know that the server isn't intending to send another line of text? (I admit, my first wording of this question wasn't brilliant) – Damien_The_Unbeliever Nov 18 '12 at 16:00
  • telnet doesn't wait for the server to finish sending output before it will send any user input, and vice versa. If you want these things to occur in parallel, I'd suggest using a separate thread for one of these activities. – Damien_The_Unbeliever Nov 18 '12 at 16:10
  • in while loop i change to `new Thread(() => { Console.WriteLine(reader.ReadLine()); }).Start(); new Thread(() => { writer.WriteLine(Console.ReadLine()); }).Start();` it not work, you can help me – NamPNQ Nov 18 '12 at 16:20

1 Answers1

1

You need to decide how this is all going to shutdown, but as indicated in the comments, I'd suggest you use a second thread, to get both sides (the send and receive sides on the client) able to run without blocking:

TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse(HOST_IP), HOST_PORT);
Stream stream = client.GetStream();
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true;
var t = new Thread(() => {
    while(true) {
        writer.WriteLine(Console.ReadLine());
    }
});
t.Start();
while (true) {
  string line;
  while((line = reader.ReadLine())!=null) {
    Console.WriteLine(line);
  }
}

Unlike in your comment, you wouldn't want to create the threads in a loop - there's always a possibility that one side (e.g. receiving from the server) can process multiple times without any work for the other side (sending to the server).

For shutdown, I'd usually create a ManualResetEvent that gets set (by whatever criteria are appropriate) when it's time to shutdown. I'd then make testing the event object the condition for the while loops.

The only issue is that Console.ReadLine() is blocking. So your read console/send to server thread wouldn't see that the shutdown event has been set unless it receives new input. If your read/send thread isn't responsible for initiating shutdown, you could make it a background thread (assuming it doesn't mutate any global state) and then just deal with shutting down your receive/write thread (i.e. the main thread) at an appropriate time.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448