简体   繁体   English

如何在 boost.asio 中打印请求?

[英]How can I print the requests in boost.asio?

I'm using boost.asio to write a simple server.我正在使用boost.asio编写一个简单的服务器。 The code below is trying to do 2 things:下面的代码试图做两件事:

  1. print the request打印请求
  2. respond to the client hello回复客户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 << '\n';
                        boost::asio::write(socket, boost::asio::buffer("hello"),err);
    
                }   
        }   
        catch (std::exception& e){ 
    
                std::cerr << e.what() << '\n';
        }   

        return 0;

}

When I run the server and send a request using curl it does not respond and only print an empty line.当我运行服务器并使用curl发送请求时,它没有响应,只打印一个空行。

[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):在服务器端(2个空行):

[amirreza@localhost 2]$ sudo ./server 
[sudo] password for amirreza: 


here I have 2 questions:在这里我有两个问题:

  • why server doesn't print the curl request?为什么服务器不打印 curl 请求?
  • why curl doesn't receive the hello message?为什么 curl 没有收到hello消息?

I also observed packets sent and received between server and curl in wireshark.我还观察了wireshark中服务器和curl之间发送和接收的数据包。 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. 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.

  1. First thing I notice:我注意到的第一件事:

     std::string buff; socket.read_some(boost::asio::buffer(buff), err);

    This reads into an empty string: 0 bytes.这读入一个空字符串:0 字节。 Either reserve space:要么预留空间:

     buff.resize(1024); socket.read_some(boost::asio::buffer(buff), err);

    or use a dynamic buffer with a composed read operation (see next)或使用具有组合读取操作的动态缓冲区(请参阅下一个)

  2. read_some reads whatever is available, not necessarily a line. read_some 读取任何可用的内容,不一定是一行。 see read_until for higher level read operations有关更高级别的读取操作,请参见read_until

     std::string buff; read_until(socket, boost::asio::dynamic_buffer(buff), "\n");
  3. handle errors.处理错误。 In your case you could simply remove the ec variable, and rely on exceptions since you alreaady handle those在您的情况下,您可以简单地删除ec变量,并依赖异常,因为您已经处理了这些

Fixes修复

#include <boost/asio/buffer.hpp>
#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(),  11010));
        for(;;) {
            tcp::socket socket(ioc);
            acceptor.accept(socket);
            std::string buff;
            auto bytes = read_until(socket, boost::asio::dynamic_buffer(buff), "\n");
            std::cout << buff.substr(0, bytes) << std::flush;
            boost::asio::write(socket,  boost::asio::buffer("hello"));
        }   
    }   
    catch (std::exception const& e){ 
        std::cerr << e.what() << '\n';
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM