简体   繁体   中英

streamreader readline blocking tcp many clients c#

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM