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?
async/awaitbut I can’t be sure, because it’s not even clear who is callingHandlePhysicalPortConnectChangeEventand when. - ZergatulReadAsyncmethod. - ZergatulC#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