2

I have written a TCP chat in C# WPF for one client and server (it works). Now I would like to extend this program to have many clients in chat.

client code: http://pastebin.com/Zv1Me6P4

server code: http://pastebin.com/VYBJCA9f

I was checking everything and I guess that streamreader readline fails.

In my program, client sends message to server, which sends to everybody and appears message in their TextBoxs.

How my program works:

  1. Start server
  2. Connect Client1, Client2
  3. Client1 sends message "a" ... nothing happens
  4. Client1 sends message "b" ... nothing happens
  5. Client2 sends message "c" ... both clients got "ac"

Streamreader blocks and I dont know how to unblock it. Okay, I can use new thread; +1 client = +1 thread, but it sounds so strange. I was really reading stackOverFlow and I found sth like: while((line = reader.ReadLine()) != null) or !reader.EndOfStream or reader.pike > 0.. all that doesn't work... or I do it incorrenctly.

Reading my code you can be confused:

  • in client program there is some server ( it's old overwritten project )
  • I create for every clients new Reader and Writer Stream; I got to know that I can use one R/W Stream but I couldn't use it however.. Because I use List list so: reader(list.getByte()) does't work.

I beg you please help me. It's small unsolved piece of my work, which makes me upset. I love programming when problems are resonable and possible to solve.

Thanks for all comment under my post.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Cenarius
  • 175
  • 1
  • 1
  • 12
  • `NetworkStream` is not like other streams. `reader.EndOfStream` will never be `true` and `ReadLine` will always block. You can't use regular stream reading examples. – Ivan Stoev Aug 15 '16 at 19:20
  • Thanks for response, If I cannot do it with it so how can I solve it? Please give me any tip. Best Regards – Cenarius Aug 15 '16 at 20:42
  • 1
    You are welcome. The space here is limited for covering the full solution. But you already seem to know the solution - try with thread per client (of course I'm talking about the server code), or try utilizing the newer async features. For instance, take a look at [Using .Net 4.5 Async Feature for Socket Programming](http://stackoverflow.com/questions/12630827/using-net-4-5-async-feature-for-socket-programming) thread. – Ivan Stoev Aug 15 '16 at 21:28
  • Before reading your post I tried new thread for new client. BackgroundWorker clientThread = new BackgroundWorker(); clientThread.WorkerSupportsCancellation = true; clientThread.DoWork += new DoWorkEventHandler(clientThread_DoWork); clientThread.RunWorkerAsync(clientNumber); Now it works :D ! Before It seemed strange, my small server and many new clients so many threads. But it's the only way. Thanks for supporting : ) ! – Cenarius Aug 15 '16 at 22:21

1 Answers1

3

I had a similar problem not being able to ReadLine and ReadToEnd would exceed my timeouts. This worked for me

string line = "";
while (reader.Peek() > -1) {
    line += (char)reader.Read();
}
Morgan
  • 31
  • 3