boost::asio::ip::tcp::socket - 如何绑定到特定的本地端口

6
我正在制作一个客户端套接字。
为了方便我的测试人员,我想指定套接字将使用的网络卡和端口。
昨天,在我的谷歌搜索中,我发现:将boost asio绑定到本地tcp端点 通过执行打开、绑定和异步连接操作,我能够绑定到特定的网络卡,并在Wireshark中开始看到流量。
然而,Wireshark报告称套接字已被赋予了随机端口,而不是我指定的端口。如果端口正在使用中,我认为它会填写传递给绑定方法的错误代码。
我做错了什么?
这是我从真实解决方案中提取和编辑的最小示例。
// Boost Includes
#include <boost/asio.hpp>
#include <boost/atomic.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/thread/condition_variable.hpp>

// Standard Includes
#include <exception>
#include <memory>
#include <string>
#include <sstream>


boost::asio::io_service        g_ioService;                     /** ASIO sockets require an io_service to run on*/
boost::thread                  g_thread;                        /** thread that will run the io_service and hence where callbacks are called*/
boost::asio::ip::tcp::socket   g_socket(g_ioService);           /** Aync socket*/
boost::asio::ip::tcp::resolver g_resolver(g_ioService);         /** Resolves IP Addresses*/

//--------------------------------------------------------------------------------------------------
void OnConnect(const boost::system::error_code & errorCode, boost::asio::ip::tcp::resolver::iterator endpoint)
{
    if (errorCode || endpoint == boost::asio::ip::tcp::resolver::iterator())
    {
        // Error - An error occured while attempting to connect
        throw std::runtime_error("An error occured while attempting to connect");
    }

    // We connected to an endpoint

    /*
    // Start reading from the socket
    auto callback = boost::bind(OnReceive, boost::asio::placeholders::error);
    boost::asio::async_read_until(g_socket, m_receiveBuffer, '\n', callback);
    */
}


//--------------------------------------------------------------------------------------------------
void Connect()
{
    const std::string hostName = "10.84.0.36";
    const unsigned int port = 1007;

    // Resolve to translate the server machine name into a list of endpoints
    std::ostringstream converter;
    converter << port;
    const std::string portAsString = converter.str();

    boost::asio::ip::tcp::resolver::query query(hostName, portAsString);

    boost::system::error_code errorCode;
    boost::asio::ip::tcp::resolver::iterator itEnd;
    boost::asio::ip::tcp::resolver::iterator itEndpoint = g_resolver.resolve(query, errorCode);

    if (errorCode || itEndpoint == itEnd)
    {
        // Error - Could not resolve either machine
        throw std::runtime_error("Could not resolve either machine");
    }

    g_socket.open(boost::asio::ip::tcp::v4(), errorCode);
    if (errorCode)
    {
        // Could open the g_socket
        throw std::runtime_error("Could open the g_socket");
    }

    boost::asio::ip::tcp::endpoint localEndpoint(boost::asio::ip::address::from_string("10.86.0.18"), 6000);
    g_socket.bind(localEndpoint, errorCode);
    if (errorCode)
    {
        // Could bind the g_socket to local endpoint
        throw std::runtime_error("Could bind the socket to local endpoint");
    }

    // Attempt to asynchronously connect using each possible end point until we find one that works
    boost::asio::async_connect(g_socket, itEndpoint, boost::bind(OnConnect, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
}

//--------------------------------------------------------------------------------------------------
void g_ioServiceg_threadProc()
{
    try
    {
        // Connect to the server
        Connect();

        // Run the asynchronous callbacks from the g_socket on this thread
        // Until the io_service is stopped from another thread
        g_ioService.run();
    }
    catch (...)
    {
        throw std::runtime_error("unhandled exception caught from io_service g_thread");
    }
}

//--------------------------------------------------------------------------------------------------
int main()
{
    // Start up the IO service thread
    g_thread.swap(boost::thread(g_ioServiceg_threadProc));

    // Hang out awhile
    boost::this_thread::sleep_for(boost::chrono::seconds(60));

    // Stop the io service and allow the g_thread to exit
    // This will cancel any outstanding work on the io_service
    g_ioService.stop();

    // Join our g_thread
    if (g_thread.joinable())
    {
        g_thread.join();
    }

    return true;
}

如下截图所示,随机端口32781被选择,而不是我请求的端口6000。

Wireshark Screen Shot

2个回答

4

我怀疑楼主不再关心这个问题,但对于像我一样的未来寻求者,这里是解决方案。

这里的问题在于boost::asio::connect在调用提供范围内的每个端点的connect之前关闭套接字:

从boost/asio/impl/connect.hpp中:

template <typename Protocol BOOST_ASIO_SVC_TPARAM,
    typename Iterator, typename ConnectCondition> 
Iterator connect(basic_socket<Protocol BOOST_ASIO_SVC_TARG>& s,
    Iterator begin, Iterator end, ConnectCondition connect_condition,
    boost::system::error_code& ec)
{ 
  ec = boost::system::error_code();

  for (Iterator iter = begin; iter != end; ++iter)
  {
    iter = (detail::call_connect_condition(connect_condition, ec, iter, end));
    if (iter != end)
    {
      s.close(ec); // <------
      s.connect(*iter, ec);
      if (!ec)
        return iter;
    }
...
} 

这就是为什么绑定地址会被重置。要保持绑定,可以直接使用 socket.connect/async_connect(...)


谢谢您跟进此事,这解决了我的问题。从未想过静态和成员版本的connect之间会有这样的差异。 - miles.sherman

2

6000是远程端口,它被正确地使用了(否则,您将无法连接服务器端)。

引用自:https://idea.popcount.org/2014-04-03-bind-before-connect/

TCP/IP连接由四个元素组成:{源IP、源端口、目标IP、目标端口}。要建立TCP/IP连接只需要目标IP和端口号,操作系统会自动选择源IP和端口。

由于您没有绑定本地端口,因此会从“临时端口范围”中随机选择一个端口。这是远程连接的通常方式。

不必担心:

It is possible to ask the kernel to select a specific source IP and port by calling bind() before calling connect()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Let the source address be 192.168.1.21:1234
s.bind(("192.168.1.21", 1234))
s.connect(("www.google.com", 80))

这个示例是使用Python编写的。

如果你尝试更改端口,但仍然得到另一个端口,那么很可能提示端口不可用。

请查看链接文章中关于SO_REUSEADDRSO_REUSEPORT的信息。


1
这是我在代码示例中所做的,但它没有起作用。在async_connect之前,请查看我的bind调用。远程端点端口为1007,在Wireshark中也是如此。在调用解析器之前指定了远程端点。6000是源端口,我像你一样通过绑定来指定它,但似乎被忽略了,因此提出了问题。在绑定之前添加g_socket.set_option(boost::asio::ip::tcp::socket::reuse_address());这行代码没有效果,我得到了相同的结果。我看不到在boost :: asio中设置reuse_port的方法。 - Christopher Pisz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接