Good day. For learning purposes, I am writing a chat on the TCP protocol. In the console version, everything is fine, the chat normally sends messages to the server, and the server sends to all clients. But once I transfer this code to WPF, that's all. The client starts sending empty requests to the server. As a result, the server is clogged up with this.

Sample server error

And in the console version, all is well All is well

Server - http://pastebin.com/c2Ru2j30

Console working version - http://pastebin.com/0707wqKu

Not working WPF - http://pastebin.com/CLpRUGKG

  • Do you have the opportunity to put the code on githab? - user227049
  • @FoggyFinder I am now rewriting this case for sockets. If there will be such a trouble, I will post it necessarily - Almightily
  • @FoggyFinder you won’t believe how stupid a programmer can be at one o'clock :) See my WPF connection code. There try-catch construction. And I still have a miracle standing there finally, in which the shutdown function ... My head hurts from the facepalm ... - Almightily
  • I believe - I had to implement some algorithms until the morning;) - user227049

1 answer 1

The decision turned out to be banal inattention. Just need to remove from the code:

try { client.Connect(HOST, PORT); //подключение клиента stream = client.GetStream(); // получаем поток string message = userName; byte[] data = Encoding.Unicode.GetBytes(message); stream.Write(data, 0, data.Length); // запускаем новый поток для получения данных Thread receiveThread = new Thread(new ThreadStart(receiveMessage)); receiveThread.Start(); //старт потока прослушивания входящих сообщений textBoxChat.AppendText("Добро пожаловать, " + userName); } catch (Exception ex) { textBoxChat.AppendText(ex.Message); } finally { disconnect(); } 

It:

  finally { disconnect(); }