如何通过超时检查Boost ASIO套接字中的数据
How can I check data in boost asio socket with timeout
如果boost :: asio :: socket的缓冲区中有一些数据,该如何阅读?支票应阻止程序的执行,直到达到超时为止。我发现可以使用该函数
boost::asio::socket_base::bytes_readable
,但似乎是一个非阻滞动作,无需检查任何超时就可以运行。我不想使用async_read,但是如果有一些数据读
您只需使该套接字非阻塞并等待插座准备好阅读或使用事件循环进行超时。
阻止程序执行的一种简便方法,等待套接字中的某些字节是在一段时间内将函数放置,以监视经过的时间。在这里,一个简单的解决方案将:
- 连接到服务器
- 从服务器读取。如果没有阅读,请等到超时
- 检查服务器消息并回复
int find_char(char* buffer,size_t size_out,char chr)
#include <boost/asio.hpp>
int main () {
const int MAX_BUFFER_SIZE = 1024; //max size of buffer
const char PACKET_END = static_cast<char>(0xFF); //character that identifies the end of a packet
//creation of Client Socket
boost::asio::io_service ios;
boost::asio::ip::tcp::socket client_socket(ios);
//creation of endpoint connection
std::string host_ip = "199.01.00.00";
int port_number = 2000;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(host_ip), port_number);
//connect client socket to server
boost::system::error_code error_connection; //connection error
//conect to server
bool is_connected = false;
client_socket.connect(endpoint, error_connection);
if(!error_connection)
is_connected = true;
//read from server buffer
if (is_connected == true) { //connected
//create empty buffer to store from server
char buffer[MAX_BUFFER_SIZE];
std::fill(buffer, buffer + sizeof(buffer), 0);
//TCPIP read from buffer
boost::system::error_code error_read;
//initialize Timeout variables
int timeout = 5000000; //timeout of 5 seconds [microsec]
boost::posix_time::time_duration difference; //elapsed time
boost::posix_time::time_duration time_to_read; //duration of reading
boost::posix_time::ptime start_try_read = boost::posix_time::microsec_clock::local_time(); //save initial time to read
//understand if there is something to read
size_t bytes_readable = 0;
bool something_to_read = false; //check if something to read
do {
boost::posix_time::ptime start_read = boost::posix_time::microsec_clock::local_time(); //save initial time to try read
boost::asio::socket_base::bytes_readable num_bytes_readable(true);
client_socket.io_control(num_bytes_readable, error_read);
bytes_readable = num_bytes_readable.get(); //get the value of readable data
if ( bytes_readable > 0 ){
//found something to read
something_to_read = true;
break;
}
if (error_read == boost::asio::error::not_connected){
//server disconnected
is_connected = false;
something_to_read = false;
break;
}
boost::posix_time::ptime end_read = boost::posix_time::microsec_clock::local_time(); //save final time to try read
time_to_read = end_read - start_read;
difference = end_read - start_try_read;
} while ( difference + time_to_read < boost::posix_time::microsec(time_out) && bytes_readable <= 0 );
if (is_connected == true && difference + time_to_read >= boost::posix_time::microsec(time_out)) {
//nothing to read, timeout
something_to_read = false;
}
else if (is_connected == true && !error_read && bytes_readable > 0) {
//prepare to store the buffer
char buffer_socket[bytes_readable + 1];
std::fill(buffer_socket, buffer_socket + sizeof(buffer_socket), 0);
//read from buffer the readable bytes amount and store
boost::asio::read(client_socket, boost::asio::buffer(buffer_socket, bytes_readable));
//check if message contains special character
int position_end = find_char(buffer_socket, sizeof(buffer_socket), static_cast<char>(PACKET_END));
//in case found an interesting message, reply to server
if (position_end >= 0){
std::memcpy(buffer, buffer_socket, position_end + 1); //found interesting message, store it
//write response to server socket
char response[100] = "received good message";
boost::system::error_code error_write;
boost::asio::write(client_socket, boost::asio::buffer((char*)ct_request_buffer, sizeof(ct_request_buffer)), error_write);
}
}
}
}
int find_char(char* buffer, size_t size_out, char chr){
int pos = -1;
size_t i = size_out - 1;
while (i >= 0){
if (buffer[i] == chr){
pos = i;
break;
}
i--;
}return pos;
}
相关文章:
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- WTSEnumerateProcesses与套接字和数据结构
- 在 1 个服务器 n 客户端套接字 C++ MFC 应用程序中更新数据的客户端
- C++ TCP 套接字通信 - 连接按预期工作,几秒钟后失败,没有收到新数据,read() 和 recv() 块
- 我们可以在套接字编程中将自定义数据作为辅助数据发送吗?
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- 尝试将数据从C++发送到 Python 并使用套接字C++反转,C++ sendto() 和 python recvfr
- 不要从输入队列套接字读取数据
- 通过套接字 c++ 发送长度和数据
- 如何读取套接字编程(c ++)中的所有数据?
- 使用 Broadcast 发出的从节点服务器发送的数据不能被 C++ 套接字 IO 客户端读取
- 无法通过套接字发送或接收数据
- 连接UDP套接字,但仍然接收来自其他源的数据报
- boost::asio-async_read_some示例代码没有读取套接字中的所有数据
- 使用 epoll 边缘触发器的套接字上的数据过多
- 为什么 sys 套接字 recv 函数不填充数据但返回字节长度?
- 将空数据写入非阻塞套接字会导致epoll_wait挂起
- C++ 套接字 - 发送 - 未收到数据
- 通过套接字发送的自定义数据包
- 无法在java(客户端)的套接字上正确写入数据(类),也无法在c ++(服务器)上读取数据(类)