Repository navigation
Closed
Description
I want to know the maximum length of a request that the server can handle, so I add a callback that prints the request length to 'hello_world_server.cpp', and started the server. then I sent JSON data from a python script to the server.
when the JSON length is 1KB, everything goes right, but when I set length to 3KB even 10KB, I get an error that is 'Connection reset by peer' in python.
Are there some options I didn't set right?
Here is my code.
Thanks a lot!
#include <boost/network/protocol/http/server.hpp>
#include <iostream>
namespace http = boost::network::http;
/*<< Defines the server. >>*/
struct hello_world;
typedef http::server<hello_world> server;
void callback(boost::iterator_range<char const *> const data,
boost::system::error_code const code, size_t size,
server::connection_ptr p) {
// std::cout << std::string(data.begin(), data.end()) << std::endl;
std::cout << "size : " << size * 1.0 / 1024 << "KB" << std::endl;
}
/*<< Defines the request handler. It's a class that defines two
functions, `operator()` and `log()` >>*/
struct hello_world {
/*<< This is the function that handles the incoming request. >>*/
void operator()(server::request const &request,
server::connection_ptr connection) {
server::string_type ip = source(request);
unsigned int port = request.source_port;
std::ostringstream data;
data << "Hello, " << ip << ':' << port << '!';
std::map<std::string, std::string> headers = {
{"Content-Length", "0"},
{"Content-Type", "text/plain"},
};
auto body = data.str();
headers["Content-Length"] = std::to_string(body.size());
connection->set_status(server::connection::ok);
connection->set_headers(headers);
connection->write(body);
connection->read(callback);
}
};
int main(int argc, char *argv[]) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
return 1;
}
try {
/*<< Creates the request handler. >>*/
hello_world handler;
/*<< Creates the server. >>*/
server::options options(handler);
boost::asio::socket_base::receive_buffer_size bufferSize(1024 * 1024);
server server_(options.address(argv[1])
.port(argv[2])
.receive_buffer_size(bufferSize)
.linger(false)
.linger_timeout(100000));
/*<< Runs the server. >>*/
server_.run();
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# File http_post.py
import urllib
import urllib2
import json
def getData(dataSize):
data=''
for i in range(dataSize*1024):
data = data + '0'
return data
def http_post():
url='http://192.168.1.7:8000'
data=getData(10)
values ={'data': data}
jdata = json.dumps(values)
req = urllib2.Request(url, jdata)
response = urllib2.urlopen(req)
return response.read()
resp = http_post()
print resp
Error message in python
$ python test.py
data size : 10.0KB
Traceback (most recent call last):
File "test.py", line 26, in <module>
resp = http_post()
File "test.py", line 24, in http_post
return response.read()
File "/usr/lib/python2.7/socket.py", line 351, in read
data = self._sock.recv(rbufsize)
File "/usr/lib/python2.7/httplib.py", line 602, in read
s = self.fp.read(amt)
File "/usr/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
socket.error: [Errno 104] Connection reset by peer
Metadata
Metadata
Assignees
Labels
No labels
Activity
carun commentedon Jan 31, 2018
The default connection buffer size is 4KiB as seen from https://github.com/cpp-netlib/cpp-netlib/blob/master/boost/network/protocol/http/server/async_connection.hpp#L51 and is not configurable at run time. 4KiB is good enough to receive the data at very high transfer rate. Increasing the connection buffer size beyond that would only increase your memory footprint and will not help you in reading the entire data from the client.
To read any data of larger size, the ideal way is not to increase your buffer size, but to read the data asynchronously. I would recommend you look at https://github.com/cpp-netlib/cpp-netlib/blob/0.13-release/libs/network/example/http/async_server_file_upload.cpp
Make sure you have at least
N + 2threads in the thread pool to handleNconcurrent requests. Otherwise the threads in the IO pool could get into deadlock.I've modified your python script to work with the async_server example above.
Running the above python script will produce the following:
carun commentedon Feb 1, 2018
Hey @HulkSun, did this work for you? If it is still not an issue, appreciate if you can close this thread.