惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
T
The Blog of Author Tim Ferriss
U
Unit 42
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
Y
Y Combinator Blog
F
Full Disclosure
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
S
SegmentFault 最新的问题
腾讯CDC
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
T
Threatpost
GbyAI
GbyAI
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
L
LangChain Blog
T
Tenable Blog
C
Cisco Blogs
T
Threat Research - Cisco Blogs
Google Online Security Blog
Google Online Security Blog

博客园 - cpunion

使用dpkt和pcap抓包 [收藏] jfwan实现的一个C++委托类 [c++] c++0x中的auto和typeof ACE_TP_Reactor的限制 哀悼18位在车祸中死去的人 对ACE_TP_Reactor定时器处理机制做一点修改。 C++编写“异步调用代理组件”的一点想法 有趣的东西:Test () () () () () () () () () (); - cpunion VC2005 Beta 2 模板偏特化有些问题 [python] and or 表达式陷阱一则。 Media Player Classic外挂字幕时间调整脚本 我们的标准化委员会网站在哪? 不就是个座嘛 answers.com真是个不错的网站 《星际之门》和《亚特兰蒂斯》总算是更新了 生成gb2312码表 - cpunion - 博客园 [假如设计一个新语言] 哪些语言特性是我想要的 写一个CopyOnWrite的通用实现(C++) - cpunion - 博客园 Python写的一个适配器类。
ACE_SOCK_Stream send和recv超时设置
cpunion · 2005-08-28 · via 博客园 - cpunion

看到很多地方都使用下面的方式来表示不等待:

ACE_Time_Value nowait (ACE_OS::gettimeofday());
peer ().send (..... &nowait);

这包括马维达译的《ACE程序员指南》。

上次测试了一下,证实这个用法是错误的,可以做一个简单的测试环境:写一个简单的echo服务器,服务端收到数据以后sleep几秒再写回peer,而客户端则在发送数据以后立即关闭peer。这种情况下,服务端send将会有Broken pipe断道错误并强行结束程序,这可以简单添加MSG_NOSIGNAL参数(LINUX下可用),使得程序不会因Broken pipe而结束。

服务端处理过程如下:

// in function handle_input
....
char buf[1024];
ACE_Time_Value nowait (ACE_OS::gettimeofday ());
int recv_len = peer.recv (buf, 1024, MSG_NOSIGNAL, &nowait);
ACE_OS::sleep (5);
peer().send (buf, recv_len, MSG_NOSIGNAL, &nowait);
...

在服务端sleep时,由于客户端断线,将导致send失败。由于使用了MSG_NOSIGNAL标志,程序不会直接DOWN掉,但那个nowait显然没有起到应有的作用,它会一直阻塞。

通过查看ACE源码(ACE.cpp)可以看到,这个值其实是相对于当前时间的长度,send和recv函数里会对这个值加上ACE_OS::gettimeofday()的值,真正超时应该是在35年以后了。。。所以这个用法是错误的,正确用法是:

// in function handle_input
....
char buf[1024];
ACE_Time_Value nowait (ACE_Time_Value::zero);
int recv_len = peer.recv (buf, 1024, MSG_NOSIGNAL, &nowait);
ACE_OS::sleep (5);
peer().send (buf, recv_len, MSG_NOSIGNAL, &nowait);
...