Enlight
Enlight

Reputation: 65

protobuf.net De-Serializer waits indefinitely

I have got a simple TCPlistener that listens for connections and once a connection has been established, a method tries to send data and the server side tries to read it.

client side:

using (NetworkStream stream = new NetworkStream(_client.Client, false))
            {
                Serializer.Serialize(stream, MyPersonObject);
            }

Server side:

using (NetworkStream stream = new NetworkStream(_client.Client, false))
            {
                var myObject = Serializer.DeSerialize<Person>(stream);
            }

However, I've noticed that once it hits the DeSerialize method, it hangs and waits indefinitely. Note that this does NOT happen with BinaryFormatter using the exact same steps. I am not sure what's wrong.

Upvotes: 3

Views: 262

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063338

A protobuf stream is not "closed" - it reads until the end of the stream by default, which means it will read until the inbound TCP socket is mark as complete.

If you intend to send multiple messages, try using the "WithLengthPrefix" versions of serialize and deserialize; that adds message framing for you, allowing it to know where each payload ends.

Upvotes: 4

Related Questions