Boost ASIO绑定:参数无效

时间:2016-07-20 01:15:01

标签: c++ sockets boost udp boost-asio

我正在尝试将端点绑定到套接字,并且我收到了这样的异常

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
  what():  bind: Invalid argument
Aborted (core dumped)

代码如下。我更愿意解决绑定问题。

#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::udp;

int main()
{
    const int port=2405;
    const std::string ip="127.0.0.1";

    boost::asio::io_service io_service;
    udp::endpoint endpoint;
    udp::socket socket(io_service, udp::endpoint(udp::v4(), 0));

    udp::resolver resolver(io_service);
    endpoint = *resolver.resolve({udp::v4(), ip, std::to_string(port)});
    socket.bind(endpoint);

    return 0;
}

请:

g++ test.cpp -Wfatal-errors -o test -std=c++11 -lboost_system -lboost_thread -lboost_serialization -pthread

0 个答案:

没有答案