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

2021-12-24 00:00:00 c++ boost boost-asio

你能在 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.

相关文章