gpt4 book ai didi

c++ - "Error: A connect request was made on an already connected socket"

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:00:41 24 4
gpt4 key购买 nike

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

代码第一次似乎工作正常,但在第二次尝试时它到达 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));

有人知道我在这种情况下应该怎么做吗?断开 socket 并重新连接?如果是这样,我该怎么做?

最佳答案

也许将 SO_REUSEADDR 选项与 setsockopt 结合使用可能会有所帮助。

关于c++ - "Error: A connect request was made on an already connected socket",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7910605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com