I have really simple TcpListener + TcpClient slient-server application. I accept incomming connections async. Then I read data from network stream.
I run this code in new thread. but I have to run new thread for each client…
while (true)
(MyCoreMessage)BinaryFormatter.Deserialize(TcpClient.GetStream())
Is there some (easy and simple) async way to desearialize object from networkstream?
Not creating new thread per clent…
thanks for help
I would strongly suggest that you preceded the data with a length-prefix (for example, as network-byte-order 4 bytes), and read this length first. Then you know how much data to expect in the next frame, so buffer that much data using async reads (assuming the size isn’t too silly large). When you have finished buffering the data (async), then you can use something like MemoryStream and deserialize normally.
This: