I do not understand how I respond to receiving data from the stream from the connection (TcpClient.GetStream ()). In the code description of the work.

The method in which we respond to the appearance of new data in the stream.

public static async Task<MessageContainer> Deserialization(TcpClient tcpClient) { // То, что 2733 это не корректно, знаю. Просто приходит пока только одно сообщение одного размера. byte[] buff = new byte[2733]; //tcpClient.Client.Receive(buff); BinaryFormatter formater = new BinaryFormatter(); Stream str = tcpClient.GetStream(); // Я как понимаю в этой строчке. await str.ReadAsync(buff, 0, 2733); MemoryStream mstr = new MemoryStream(buff); // Раньше работало так. При подключении просто была эта строка, которая читала из NetworkStream. Возможно внутри он его закрывает,но // под Debug поток открыт еще. var answer = (MessageContainer)formater.Deserialize(mstr); Console.WriteLine(answer.Data.DataObjects.SingleOrDefault()); return answer; } 

Question:

How to respond to the appearance of new data in the stream?

I need to loop this method?

  • I know that the working methods do not actually do what is necessary (in terms of why Deserialization responds to receiving data, rather than according to the method). Interested in how to respond at all. - Arantler
  • Cut off all unnecessary and make minimally reproducible code. I suspect you have an error using async/await but I can’t be sure, because it’s not even clear who is calling HandlePhysicalPortConnectChangeEvent and when. - Zergatul
  • Perhaps the problem is that you do not check the return value of the ReadAsync method. - Zergatul
  • @Zergatul OnPhysicalPortConnectChangeEvent // in the PhysicalPort class calls HandlePhysicalPortConnectChangeEvent - Arantler
  • one
    TCP sends the length of the packets, but not the length of the messages you send in the C# code. You will have to send the length yourself, as TCP can fragment your message at its discretion. In any case, C# classes do not provide such low-level control over TCP. - Zergatul

0