我有一个使用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) 以下是我的代码
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.仍无法在指定时间内超时
可能重复:
如何在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超时吗?
根据示例创建一个简单的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毫秒超时并抛出异常?
我正在改编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 是否有能力在阻塞连接/读/写调用上使用超时。
boost-asio ×6
c++ ×5
boost ×4
sockets ×2
timeout ×2
boost-beast ×1
linux ×1
synchronous ×1
tcp ×1