我正在做一个来自 www.highscore.de 的关于 Boost.Asio 的教程。这是我正在尝试运行的示例:
#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();
}
但每次它在这一行抛出异常:
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);
查看堆栈跟踪,basic_socket_acceptor 中的这一行抛出:
boost::asio::detail::throw_error(ec, "bind");
有谁知道为什么?