I'm using boost.asio
to write a simple server. The code below is trying to do 2 things:
- print the request
- respond to the client
hello
but it does not.
#include <iostream>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main(){
try{
boost::asio::io_context ioc;
tcp::acceptor acceptor(ioc, tcp::endpoint(tcp::v4(), 1010));
for(;;){
tcp::socket socket(ioc);
acceptor.accept(socket);
boost::system::error_code err;
std::string buff;
socket.read_some(boost::asio::buffer(buff), err);
std::cout << buff << '
';
boost::asio::write(socket, boost::asio::buffer("hello"),err);
}
}
catch (std::exception& e){
std::cerr << e.what() << '
';
}
return 0;
}
When I run the server and send a request using curl
it does not respond and only print an empty line.
[amirreza@localhost ~]$ curl 127.0.0.1:1010
curl: (1) Received HTTP/0.9 when not allowed
[amirreza@localhost ~]$
and in server side (2 empty line):
[amirreza@localhost 2]$ sudo ./server
[sudo] password for amirreza:
here I have 2 questions:
- why server doesn't print the curl request?
- why curl doesn't receive the
hello
message?
I also observed packets sent and received between server and curl in wireshark. At first the tcp handshake will occur but when curl send the HTTP
request, the server respond a tcp packet with RST
flag to reset the connection.
question from:
https://stackoverflow.com/questions/65882464/how-can-i-print-the-requests-in-boost-asio 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…