如何设置在升压asio的阻止套接字超时? [英] How to set a timeout on blocking sockets in boost asio?

查看:181
本文介绍了如何设置在升压asio的阻止套接字超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法取消挂起的操作(没有断开连接)或设置为升压库函数超时?

即。我想设置一个超时的升压ASIO阻挡套接字?

socket.read_some(提高:: ASIO ::缓​​冲区(pData所,MAXSIZE),错误_);

例如:我想从插座看了一些,但我想如果在10秒过去了抛出一个错误


解决方案

在Linux / BSD上的插座上的I / O操作的超时时间直接由操作系统支持

。该选项可以通过 setsocktopt启用()。我不知道,如果的boost :: ASIO 提供了一种方法来设置它还是暴露了插座scriptor允许您直接设置 - 后一种情况是不是真的便携式。

有关此完整性清酒是从该名男子页的描述:


  

SO_RCVTIMEO SO_SNDTIMEO

 指定接收或发送超时,直到报告的
          错误。该参数是timeval结构。如果输入或输出
          功能块这一段时间,和数据已被发送
          或接收,该函数的返回值将是
          传输的数据的量;如果没有数据被传输和
          在达到超时则返回-1,并将errno设置
          为EAGAIN或EWOULDBLOCK就像套接字被指定
          是非阻塞的。如果超时设置为零(缺省值)
          然后该操作将永远不会超时。超时只有
          系统调用执行套接字I / O(例如,读取(2)的影响,
          recvmsg(2),送(2),SENDMSG(2));超时没有对效果
          选择(2),民意调查(2),epoll_wait(2)等。


Is there a way to cancel a pending operation (without disconnect) or set a timeout for the boost library functions?

I.e. I want to set a timeout on blocking socket in boost asio?

socket.read_some(boost::asio::buffer(pData, maxSize), error_);

Example: I want to read some from the socket, but I want to throw an error if 10 seconds have passed.

解决方案

Under Linux/BSD the timeout on I/O operations on sockets is directly supported by the operating system. The option can be enabled via setsocktopt(). I don't know if boost::asio provides a method for setting it or exposes the socket scriptor to allow you to directly set it -- the latter case is not really portable.

For a sake of completeness here's the description from the man page:

SO_RCVTIMEO and SO_SNDTIMEO

          Specify the receiving or sending  timeouts  until  reporting  an
          error.  The argument is a struct timeval.  If an input or output
          function blocks for this period of time, and data has been  sent
          or  received,  the  return  value  of  that function will be the
          amount of data transferred; if no data has been transferred  and
          the  timeout has been reached then -1 is returned with errno set
          to EAGAIN or EWOULDBLOCK just as if the socket was specified  to
          be  non-blocking.   If  the timeout is set to zero (the default)
          then the operation  will  never  timeout.   Timeouts  only  have
          effect  for system calls that perform socket I/O (e.g., read(2),
          recvmsg(2), send(2), sendmsg(2)); timeouts have  no  effect  for
          select(2), poll(2), epoll_wait(2), etc.

这篇关于如何设置在升压asio的阻止套接字超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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