C++ Google Protocol Buffers open http socket

SO helpful folks:

This is another NEWBIE question. I am new at C++, Google Protocol Buffers, and serializing messages over HTTP without SOAP (even with SOAP for that matter). I am trying to send image data over http. I was told that Google Protocol Buffers was the way to go. So I eagerly started going through all of the documentation and used CocoaPods to install and include Google Protocol Buffer classes to my project. I created my .protoc files and generated the classes. Now I'm populating the data using the generated classes. Now what???

I cannot find any information on how to send the data. I have found a few other questions on SO that contained what might be meaningful information if I had a clue. This is what I have so far:

void message::myMessage::transmit(const uint32_t ipaddress, uint32_t port, message::MatMessage* rawImage)
{
    message::HostMessage *transmitter;
    transmitter->set_ipaddress(ipaddress);
    transmitter->set_port(port);

    //open socket with ip and port

    //send unsing socket

}

HostMessage (*transmitter) is generated C++ class and header from a .protoc file that contains only the ipAddress and port number. OK, so I have this transmitter with an ipaddress and a port. Now what? How do I open a socket with it?

Maybe once I open a socket the other answers will make more sense to me. Can someone please help unconfuse me?

Thank you.


Protocol Buffers is just a serialization tool. It will convert your message object into bytes, and will convert bytes back into a message object. The library does not directly implement a way to transport those bytes.

If you have a raw socket, you could wrap it in FileInputStream / FileOutputStream to read/write protobufs from/to it*. Since protobufs are not self-delimiting, you need to write the size as a prefix, followed by the data, and interpret these correctly on the receiving end. See my answer to this other question for code to do that: Are there C++ equivalents for the Protocol Buffers delimited I/O functions in Java?

Another alternative is to use a higher-level transport library like ZeroMQ, which implements sending and receiving of byte-blob messages. Use protobufs to encode/decode the byte blobs, then hand them off to ZeroMQ for transport.

* This works on Unix, where sockets are file descriptors. On Windows, they aren't, so you'll need to implement the ZeroCopyInputStream / ZeroCopyOutputStream directly in terms of send() / recv() , which is not too hard if you use CopyingInputStreamAdapter / CopyingOutputStreamAdaptor .

链接地址: http://www.djcxy.com/p/48334.html

上一篇: 从协议缓冲区创建一个python字典,像对象用于熊猫

下一篇: C ++谷歌协议缓冲区打开http套接字