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.

4
  • 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. Commented Aug 15, 2016 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 Commented Aug 15, 2016 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 thread. Commented Aug 15, 2016 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 : ) ! Commented Aug 15, 2016 at 22:21

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.