如何获取套接字的响应
How to get the response of the socket?
我在boost_asio
的帮助下开发了简单的跟踪路由程序。我用这个例子。我很少更改此示例以实现traceroute
而不是ping
。
pinger(boost::asio::io_service& io_service, const char* destination)
: resolver_(io_service), socket_(io_service, icmp::v4()),
timer_(io_service), sequence_number_(0), num_replies_(0)
{
boost::asio::ip::unicast::hops option(%1%); // 1 is a param
socket_.set_option(option);
icmp::resolver::query query(icmp::v4(), destination, "");
destination_ = *resolver_.resolve(query);
start_send();
start_receive();
}
我有一个问题。当time to live
少于它需要时,我没有任何反应。我希望得到这样的东西:
C:Users>ping 91.92.100.254 -i 2
Pinging 91.92.100.254 with 32 bytes of data:
Reply from 10.110.50.251: TTL expired in transit.
Reply from 10.110.50.251: TTL expired in transit.
Reply from 10.110.50.251: TTL expired in transit.
我没有经常使用boost asio(或boost,就此而言),所以我不确定TTL过期错误会发送到哪个处理程序,但这应该让你走上正确的轨道......
在开始等待回复的位置,添加 _1 占位符以将套接字错误传递给 handle_receive 方法:
// Wait for a reply. We prepare the buffer to receive up to 64KB.
// Note that you can use boost::asio::placeholders::error in place of _1
// and boost::asio::bytes_transferred in place of _2.
socket_.async_receive(reply_buffer_.prepare(65536),
boost::bind(&pinger::handle_receive, this, _1, _2));
同时更改handle_receive标头以接受错误代码
void handle_receive(const boost::system::error_code& error, std::size_t length) {
if (error) {
// Handle error
}
else {
// Handle normally
}
}
错误可能会传递到超时处理程序中。在这种情况下,您也可以检查其中的错误代码:
// Add _1 placeholder
timer_.async_wait(boost::bind(&pinger::handle_timeout, this, _1));
同样,您需要将该参数添加到处理程序中:
void handle_timeout(const boost::system::error_code& error) {
if (error) {
// Handle error
}
else {
// Handle normally
}
}
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- 套接字读取后,我在缓冲区中看到意外输入
- 如何在CPP中创建应该在Windows和Linux上运行的套接字?
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- 使用RAW套接字读取ICMP响应
- 使用套接字 SFML 时窗口无响应
- 使用套接字编程的怪异HTTP响应
- 通过套接字正确发送 HTTP 响应
- 无法使用原始套接字打印 TCP 响应
- 如何获取套接字的响应
- 通过原始套接字接收"syn"数据包时,如何使用"syn ack"数据包进行响应?
- 如何在一个套接字上实现并行请求和响应的非阻塞客户端-服务器通信模型,而不存在数据竞争
- 在连接的套接字上进行UDP响应
- 套接字编程中的多个请求/响应
- 在c++ . net中使用套接字时无法获得响应
- 在winsock中制作了一个c++套接字,并连接到谷歌,我在响应结束时得到了一些垃圾