Boost 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
}
}
如何接收 10000 个字节并执行直到所有 10000 个字节到达或 1000 毫秒超时并抛出异常?
Creating a simple TCP server based on examples but still do not get how to create a socket that would read some amount of bytes and if there will not be enough would wait. I need this to be NOT asynchronous operation.
#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
}
}
How to receive 10000 bytes and do it either until all 10000 arrive OR 1000 millisecond timeout and throw an exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Boost 1.47.0 刚刚为
basic_socket_iostream
,即
expires_at
和expires_from_now
方法。这是一个基于您的代码片段的示例:
这个程序在 Linux 上适用于我。
请注意,我并不提倡您使用超时来代替带有截止时间计时器的异步操作。由您决定。我只是想表明
basic_socket_iostream
可以实现超时。Boost 1.47.0 just introduced a timeout feature for
basic_socket_iostream
, namely, theexpires_at
andexpires_from_now
methods.Here's an example based on your snippet:
This program works for me on Linux.
Please note that I'm not advocating that you use timeouts instead of asynchronous operation with a deadline timer. It's up to you to decide. I just wanted to show that timeouts are possible with
basic_socket_iostream
.一般来说,在使用异步方法时,您只会遇到超时和可取消性。有一些特定于平台的方法可以解决此问题,这些方法已在之前的 问题。我强烈建议您使用异步方法进行研究,这将使理解逻辑变得更加容易。
也就是说,您可以通过使用下面的异步方法来呈现同步接口。这是在阻塞 TCP 客户端超时 由 Asio 库提供的示例。但请注意,由于其控制反转和 lambda 函数的使用,该示例并不完全简单(尽管有很好的注释)。我建议您在尝试此类操作之前直接研究使用异步方法。
使用
async_read
免费功能,备注部分准确描述了您所需要的其中缓冲区大小为 10,000 字节。
Generally speaking, you only get timeouts and cancelability when using asynchronous methods. There are some platform specific ways around this, which have been discussed on SO in previous questions. I strongly suggest you investigate using asynchronous methods, it will make understanding the logic much easier.
That said, you can present a synchronous interface by using asynchronous methods underneath. This is done in the blocking TCP client timeout example provided by the Asio library. Beware however, the example isn't entirely straightforward (albeit, well commented) due to its inversion of control and use of lambda functions. I suggest you investigate using asynchronous methods directly prior to attempting something like this.
use the
async_read
free function, the remarks section describes exactly what you needwhere your buffer is 10,000 bytes in size.