“错误:在已连接的套接字上发出连接请求”

时间:2011-10-27 00:51:51

标签: c++ sockets boost-asio

我正在尝试编写一个程序,它会从作为服务器的摄像机请求某些信息,例如内部温度,对不起,如果我的术语关闭,但基本上我的意思是与...摄像头是通过HTTP get请求实现的。该程序基本上只是从boost库中调用一个稍微适应的版本async_client,每次都传递一个不同的路径来检查不同的值,然后比较返回的值以确保它在通过不同的路径检查之前听起来合理其他

代码似乎第一次正常工作,但在第二次尝试时它会进入handle_connect函数并输出“错误:在已连接的套接字上发出连接请求”

这是函数的样子:

void Async_client::handle_connect(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator)
  {
        http_deadline_timer_.cancel();
        if (!err)
        {
            // The connection was successful. Send the request.
            boost::asio::async_write(socket_, request_, boost::bind(&Async_client::handle_write_request, this,
              boost::asio::placeholders::error));
            //deadline timer stuff
            http_deadline_timer_.expires_from_now( ::boost::posix_time::seconds(1));
            http_deadline_timer_.async_wait( ::boost::bind(&Async_client::call_handle_deadline_timeout, this, boost::asio::placeholders::error));
        }
        else if (endpoint_iterator != tcp::resolver::iterator())
        {
            // The connection failed. Try the next endpoint in the list.
            socket_.close();
            tcp::endpoint endpoint = *endpoint_iterator;
            socket_.async_connect(endpoint, boost::bind(&Async_client::handle_connect, this,
              boost::asio::placeholders::error, ++endpoint_iterator));
            //deadline timer stuff
            http_deadline_timer_.expires_from_now( ::boost::posix_time::seconds(1));
            http_deadline_timer_.async_wait( ::boost::bind(&Async_client::call_handle_deadline_timeout, this, boost::asio::placeholders::error));
        }
        else
        {
            LOG_WARN("Async_client.cpp: Error: " << err.message()); 
        }
  }

我尝试将else if语句中的这段代码复制到else语句中,并认为它会关闭套接字并重新运行handle_connect函数,但它似乎崩溃到endpoint = * endpoint_iterator行上的软件

socket_.close();
            tcp::endpoint endpoint = *endpoint_iterator;
            socket_.async_connect(endpoint, boost::bind(&Async_client::handle_connect, this,
              boost::asio::placeholders::error, ++endpoint_iterator));

有谁知道在这种情况下我应该做些什么?断开插座并重新连接?如果是这样,我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用SO_REUSEADDR选项和setsockopt可能有所帮助。

相关问题