你能在 boost asio 中设置 SO_RCVTIMEO 和 SO_SNDTIMEO 套接字选项吗? [英] can you set SO_RCVTIMEO and SO_SNDTIMEO socket options in boost asio?

查看:17
本文介绍了你能在 boost asio 中设置 SO_RCVTIMEO 和 SO_SNDTIMEO 套接字选项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能在 boost asio 中设置 SO_RCVTIMEO 和 SO_SNDTIMEO 套接字选项吗?

can you set SO_RCVTIMEO and SO_SNDTIMEO socket options in boost asio?

如果有,怎么办?

注意我知道你可以使用定时器来代替,但我想特别了解这些套接字选项.

Note I know you can use timers instead, but I'd like to know about these socket options in particular.

推荐答案

当然可以!Boost ASIO 允许您访问本机/底层数据,在这种情况下是 SOCKET 本身.所以,假设您有:

Absolutely! Boost ASIO allows you to access the native/underlying data, which in this case is the SOCKET itself. So, let's say you have:

boost::asio::ip::tcp::socket my_socket;

假设您已经调用了 openbind 或一些实际上使 my_socket 可用的成员函数.然后,要获取底层 SOCKET 值,请调用:

And let's say you've already called open or bind or some member function that actually makes my_socket usable. Then, to get the underlying SOCKET value, call:

SOCKET native_sock = my_socket.native();
int result = SOCKET_ERROR;

if (INVALID_SOCKET != native_sock)
{
    result = setsockopt(native_sock, SOL_SOCKET, <the pertinent params you want to use>);
}

所以你有它!Boost 的 ASIO 使您可以比其他方式更快地完成许多事情,但仍有很多事情您仍然需要正常的套接字库调用.这恰好是其中之一.

So there you have it! Boost's ASIO lets you do many things more quickly than you otherwise might be able to, but there are a lot of things you still need the normal socket library calls for. This happens to be one of them.

这篇关于你能在 boost asio 中设置 SO_RCVTIMEO 和 SO_SNDTIMEO 套接字选项吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆