asio绑定到本地tcp端点

asio绑定到本地tcp端点

本文介绍了将boost asio绑定到本地tcp端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将boost asio tcp套接字绑定到本地网络接口。何时是在套接字上调用 bind()方法的正确时间?

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指向远程ip / port,例如192.168.0.15:8888。

_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));

MyTransceiver :: handleConenct()中,我尝试了以下代码:

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);

此处调用失败,在 async_connect()调用,无效句柄异常。

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

推荐答案

看起来没有足够的信息。但一般来说,你应该:

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

_socket-> open()

_socket->open()

_socket-> set_option()

_socket->set_option()

_socket-> bind()

_socket->bind()

_socket-> async_connect()

_socket->async_connect()

:_socket-> async_read_some()

in handleConnect(): _socket->async_read_some()

这篇关于将boost asio绑定到本地tcp端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 20:29