相关疑难解决方法(0)

我是否可以使用Boost.Asio同步读取套接字并在多线程I/O服务上超时?

我有一个使用Boost.Asio进行TCP和UDP套接字通信的应用程序.我知道"Asio"中的"A"代表异步,因此库倾向于鼓励您尽可能使用异步I/O. 我有一些情况下,优选同步套接字读取.但是,与此同时,我想在所述接收调用上设置超时,因此不可能无限期地进行读取阻塞.

这似乎是Boost.Asio用户中一个非常常见的问题,以下是关于该主题的以下Stack Overflow问题:

甚至可能还有更多.文档中甚至有关于如何使用超时实现同步操作的示例.他们归结为将同步操作转换为异步操作,然后与a并行启动asio::deadline_timer.然后,计时器的到期处理程序可以在超时到期时取消异步读取.这看起来像这样(从上面链接的示例中获取的片段):

std::size_t receive(const boost::asio::mutable_buffer& buffer,
      boost::posix_time::time_duration timeout, boost::system::error_code& ec)
  {
    // Set a deadline for the asynchronous operation.
    deadline_.expires_from_now(timeout);

    // Set up the variables that receive the result of the asynchronous
    // operation. The error code is set to would_block to signal that the
    // operation is incomplete. Asio guarantees that its asynchronous
    // operations will never …
Run Code Online (Sandbox Code Playgroud)

sockets timeout synchronous boost-asio

8
推荐指数
1
解决办法
5130
查看次数

SO_RCVTIME和SO_RCVTIMEO不会影响Boost.Asio操作

以下是我的代码

boost::asio::io_service io;
boost::asio::ip::tcp::acceptor::reuse_address option(true);
boost::asio::ip::tcp::acceptor accept(io);
boost::asio::ip::tcp::resolver resolver(io);
boost::asio::ip::tcp::resolver::query query("0.0.0.0", "8080");
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
accept.open(endpoint.protocol());
accept.set_option(option);
accept.bind(endpoint);
accept.listen(30);

boost::asio::ip::tcp::socket ps(io);

accept.accept(ps);

struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
//setsockopt(ps.native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
setsockopt(ps.native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
char buf[1024];
ps.async_receive(boost::asio::buffer(buf, 1024), boost::bind(fun));
io.run();
Run Code Online (Sandbox Code Playgroud)

当我使用Telnet连接但不发送数据时,它不会与Telnet超时断开连接.是否需要设置setsockopt?谢谢!

我已将SO_RCVTIMEO修改为SO_SNDTIMEO.仍无法在指定时间内超时

c++ linux boost boost-asio

7
推荐指数
1
解决办法
3863
查看次数

提升asio超时

可能重复:
如何在boost asio中阻止套接字设置超时?

关于超时之前我读了一些条目,但我不明白.

我想要一个定义的连接超时.连接代码如下:

try{
  boost::asio::ip::tcp::resolver              resolver(m_ioService);
  boost::asio::ip::tcp::resolver::query       query(link.get_host(), link.get_scheme());
  boost::asio::ip::tcp::resolver::iterator    endpoint_iterator = resolver.resolve(query);
  boost::asio::ip::tcp::resolver::iterator    end;
  boost::system::error_code                   error   =   boost::asio::error::host_not_found;

  while (error && endpoint_iterator != end)
   {
    m_socket.close();
    m_socket.connect(*endpoint_iterator++, error);
   }
}
Run Code Online (Sandbox Code Playgroud)

我也想要读取超时.

我用它boost::asio::read_until(m_socket, response, "\r\n");来读取标题.

可以设置SIMPLE超时吗?

c++ boost timeout boost-asio

6
推荐指数
2
解决办法
1万
查看次数

提升ASIO套接字读取N个字节不多也不少,等到它们到来或超时异常?

根据示例创建一个简单的TCP服务器,但仍然没有得到如何创建一个读取一定数量字节的套接字,如果没有足够的将等待.我需要这不是异步操作.

#include <iostream>
#include <boost/asio.hpp>

#ifdef _WIN32
#include "Windows.h"
#endif

using namespace boost::asio::ip;
using namespace std;

int main(){
    int m_nPort = 12345;
    boost::asio::io_service io_service;
    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));

    cout << "Waiting for connection..." << endl;

    tcp::socket socket(io_service);
    acceptor.accept(socket);
    cout << "connection accepted" << endl;
    try
    {
        socket.send(boost::asio::buffer("Start sending me data\r\n"));
    }
    catch(exception &e)
    {
        cerr << e.what() << endl; //"The parameter is incorrect" exception
    }
}
Run Code Online (Sandbox Code Playgroud)

如何接收10000个字节并执行它直到所有10000到达或1000毫秒超时并抛出异常?

c++ sockets boost boost-asio

6
推荐指数
1
解决办法
5156
查看次数

boost::beast 同步 http 客户端超时

我正在改编Boost Beast 示例中的同步 HTTP 客户端。不幸的是,示例客户端不包含超时选项,有时会陷入我的工作负载中。我尝试添加超时

beast::get_lowest_layer(stream).expires_after(NetworkSettings::BASIC_TIMEOUT);
Run Code Online (Sandbox Code Playgroud)

在调用写/读操作之前,但这些似乎仅在使用 async_read/write 时才起作用。根据我的发现,基本的 boost asio 似乎仅支持异步操作的超时。所以我的问题是,beast 是否有能力在阻塞连接/读/写调用上使用超时。

c++ boost boost-asio boost-beast

4
推荐指数
1
解决办法
4944
查看次数

使用boost :: asio配置TCP keep_alive

Linux和Windows都支持TCP keep-alive数据包.它们可以通过(系统相关的)setsockopt调用来激活和配置,例如参见本文的Linux案例.使用boost::asio时似乎支持保持活动消息,请参阅当前文档.但该页面仅涵盖激活它.在对旧帖子的几个新回复中,有人指出Boost最近添加了配置操作超时的方法(这消除了对不同系统的需求setsockopt#ifdef代码分支).但是,最近的响应仍然建议调用本机套接字.

我的问题是:如何配置保持活动包的时间间隔和超时boost::asio

c++ tcp boost-asio

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

boost-asio ×6

c++ ×5

boost ×4

sockets ×2

timeout ×2

boost-beast ×1

linux ×1

synchronous ×1

tcp ×1