views:

85

answers:

3

I know it can be used to send/receive structured object from file,

but can it be used to send/receive sequences of structured object from a socket?

http://code.google.com/p/protobuf/

+1  A: 

I'm not familiar with protobuf, but the documentation says you can create a FileInputStream (which can then be used to create a CodedInputStream) using a file descriptor. If you're on a system that supports BSD sockets, you should presumably be able to give it a socket file descriptor rather than an ordinary one.

David
+3  A: 

Protocol Buffers is a structured data serialization (and de-serialization) framework. It is only concerned with encoding a selection of pre-defined data types into a data stream. What you do with that stream is up to you. To quote the wiki:

If you want to write multiple messages to a single file or stream, it is up to you to keep track of where one message ends and the next begins. The Protocol Buffer wire format is not self-delimiting, so protocol buffer parsers cannot determine where a message ends on their own. The easiest way to solve this problem is to write the size of each message before you write the message itself. When you read the messages back in, you read the size, then read the bytes into a separate buffer, then parse from that buffer.

So yes, you could use it to send/receive multiple objects via a socket but you have to do some extra work to differentiate each object stream.

Arnold Spence
Can you provide a sample using protobuf to deal with streams of objects?
wamp
The api actually has a class that helps with this by writing some identifier bytes and padding each object stream with size bytes to help identifying each object on the other side. The class will also handle the decoding. You can see some very brief example code here:http://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.io.coded_stream.html
Arnold Spence
A: 

Protocol Buffers does not handle any surrounding network/file I/O operations. You might want to consider using Thrift, which includes socket communication libraries and server libraries with the serialization/deserialization.

Sanketh I