Binding boost asio to local tcp endpoint

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 05:28:26

问题


I'm trying to bind a boost asio tcp socket to a local network interface specifically. When is the correct time to call the bind() method on the socket?

_endpoint points to the remote ip/port, e.g. 192.168.0.15:8888.

// Invoke async. connect. Immediate return, no throw.
_socket.async_connect(_endpoint,
boost::bind(&MyTransceiver::handleConnect, this,
    boost::asio::placeholders::error));

Within MyTransceiver::handleConenct(), I tried the following code:

boost::asio::ip::tcp::endpoint local_end_point(
        boost::asio::ip::address::from_string("192.168.0.55"), 6543 );

    _socket.bind(local_end_point);

Calling it here fails, calling it before the async_connect() call also, with a "invalid handle" exception.


回答1:


Looks like there isn't enough information. But generally, you should:

_socket->open()

_socket->set_option()

_socket->bind()

_socket->async_connect()

in handleConnect(): _socket->async_read_some()



来源:https://stackoverflow.com/questions/8416917/binding-boost-asio-to-local-tcp-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!