简体   繁体   English

Boost.Asio应用程序在创建接收器对象时引发异常

[英]Boost.Asio app throws exception when creating an acceptor object

I am doing a tutorial from www.highscore.de regarding Boost.Asio. 我正在从www.highscore.de进行有关Boost.Asio的教程。 This is the example I am trying to run: 这是我尝试运行的示例:

#include <boost/asio.hpp>
#include <string>

boost::asio::io_service io_service;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 80);
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);
boost::asio::ip::tcp::socket sock(io_service);
std::string data = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, world!";

void write_handler(const boost::system::error_code &ec, std::size_t bytes_transferred)
{
}

void accept_handler(const boost::system::error_code &ec)
{
    if (!ec)
    {
        boost::asio::async_write(sock, boost::asio::buffer(data), write_handler);
    }
}

int main()
{
    acceptor.listen();
    acceptor.async_accept(sock, accept_handler);
    io_service.run();
} 

But everytime it throws on exception on this line: 但是每次它都会在此行引发异常:

boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);

Looking at the stack trace, this line inside basic_socket_acceptor throws: 查看堆栈跟踪,basic_socket_acceptor内部的这一行抛出:

boost::asio::detail::throw_error(ec, "bind");

Does anyone has an idea why ? 有谁知道为什么?

If you can catch the exception, you'll probably get more information by calling the what() method. 如果可以捕获该异常,则可以通过调用what()方法来获取更多信息。 You'll find this easier if you move all of those global variables into a function and run from there. 如果将所有这些全局变量都移到一个函数中并从那里运行,您会发现这更容易。 That way, you can catch the exception more easily. 这样,您可以更轻松地捕获异常。

That said, on most systems you're not allowed to bind to port 80 as a non-privileged user. 也就是说,在大多数系统上,不允许您以非特权用户身份绑定到端口80。 Try something else, and see if you have more luck. 尝试其他方法,看看是否还有更多的运气。

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

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