Sending and receiving binary data

637 views
Skip to first unread message

sanikumbh

unread,
May 8, 2012, 10:33:15 AM5/8/12
to The C++ Network Library
Hi,

I am new to the cpp-netlib library and looking for some help for
sending and receiving binary data. I need to initiate a POST request
with a pay-load of some binary data. The server is going to de-
serialize this data, perform some computations and send back response
with binary data which I will be de-serializing on the client side.

Any help will be highly appreciated.

Thanks
Sachin

Dean Michael Berris

unread,
May 8, 2012, 11:41:14 PM5/8/12
to cpp-n...@googlegroups.com
You can put your binary data as the body of a request and that should
work fine. You can also use the client's post(request, binary_data,
content_type) member function to do it.

If you're writing the server too you can follow any of the examples
and deal with the request object from the handler like any other data.

Cheers

--
Dean Michael Berris
Technical Solutions Engineer
Google

sanikumbh

unread,
May 9, 2012, 2:46:36 PM5/9/12
to The C++ Network Library
Hi Dean,

Thanks for the reply. I have been trying to get something to work
following your advise to no avail. I was wondering if you could point
me to any specific examples on either of the approaches that you have
mentioned.

I am providing a snippet of code that I have written:

*****************************************
typedef http::basic_client<http::tags::http_default_8bit_tcp_resolve,
1, 1> httpClient;
typedef http::basic_request<http::tags::http_default_8bit_tcp_resolve>
httpRequest;
typedef
http::basic_response<http::tags::http_default_8bit_tcp_resolve>
httpResponse;

httpClient client;
httpRequest request("http://snikumbh-deb5-64:9910/mtsexample/
returninput");
request << header("Content-Type","application/x-google-protobuf");
unsigned char* serializedData = getData(); // Get the binary data that
needs to be POST'ed
*****************************************
I want to POST this data as the request pay-load. The server is not
using cpp-netlib. It's implemented internally.

Thanks in advance,
Sachin

On May 8, 11:41 pm, Dean Michael Berris <dber...@google.com> wrote:

Dean Michael Berris

unread,
May 10, 2012, 2:03:50 AM5/10/12
to cpp-n...@googlegroups.com
On Thu, May 10, 2012 at 4:46 AM, sanikumbh <sani...@gmail.com> wrote:
> Hi Dean,
>
> Thanks for the reply. I have been trying to get something to work
> following your advise to no avail. I was wondering if you could point
> me to any specific examples on either of the approaches that you have
> mentioned.
>

Unfortunately I can't. Maybe others on the list can?

> I am providing a snippet of code that I have written:
>
> *****************************************
> typedef http::basic_client<http::tags::http_default_8bit_tcp_resolve,
> 1, 1> httpClient;
> typedef http::basic_request<http::tags::http_default_8bit_tcp_resolve>
> httpRequest;
> typedef
> http::basic_response<http::tags::http_default_8bit_tcp_resolve>
> httpResponse;
>
> httpClient client;
> httpRequest request("http://snikumbh-deb5-64:9910/mtsexample/
> returninput");
> request << header("Content-Type","application/x-google-protobuf");
> unsigned char* serializedData = getData(); // Get the binary data that
> needs to be POST'ed
> *****************************************
> I want to POST this data as the request pay-load. The server is not
> using cpp-netlib. It's implemented internally.
>

There are a couple of problems here:

1. Since the data is an `unsigned char *` you may have to encode this
in something that's 7-bit clear format like Base64 or some variant
thereof. cpp-netlib only supports data that's encapsulated in a
std::string as of version 0.9.4.

2. You may be leaking memory if you don't clean up serializedData
properly (but you may already know that).

It would help you a lot if you check the online documentation for
hints: http://cpp-netlib.github.com/.

Dean Michael Berris

unread,
Mar 18, 2013, 8:50:44 PM3/18/13
to cpp-n...@googlegroups.com
On Sun, Mar 17, 2013 at 7:08 AM, Michael Martin Moro
<sexym...@gmail.com> wrote:
> Actually, I use 0.9.4 and sending binary data encapsulated in a std::string
> does not work for me.
>
> Here's the code I use to send a binary file to an HTTP client requesting it:
> bool SendBinaryFile(const string& filepath, Server::response& response)
> {
> ifstream file(filepath.c_str(), std::ios::binary);
>
> if (file.is_open())
> {
> long size, begin, end;
> char* raw;
>
> begin = file.tellg();
> file.seekg(0, std::ios::end);
> end = file.tellg();
> file.seekg(0, std::ios::beg);
> size = end - begin;
> raw = new char[size + 1];
> file.read(raw, size);
> file.close();
> response = Server::response::stock_reply(Server::response::ok, raw);

Here is your problem -- you're making a temporary std::string in
stock_reply. What you want to do is this instead:

std::string binary(raw, size);
response = Server::response::stock_reply(Server::response::ok, binary);
Reply all
Reply to author
Forward
0 new messages