[linux] 将socket设置为非阻塞(non-blocking)
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash

·
有一个非常有迷惑性的做法是:
u_long has = 1;
ioctl(m_sock, FIONBIO , &has);
这个函数会非常无耻的返回你success,但是它实际上很可能什么也没做。
正确的做法应该是使用fcntl:
int flags = fcntl(m_sock, F_GETFL, 0);
fcntl(m_sock, F_SETFL, flags|O_NONBLOCK);
这真是一个隐蔽的问题,折腾了我两天。线程每每停留在send()调用那里,我始终没怀疑到:用ioctl设置FIONBIO成功之后,socket竟然还是阻塞的。
u_long has = 1;
ioctl(m_sock, FIONBIO , &has);
这个函数会非常无耻的返回你success,但是它实际上很可能什么也没做。
正确的做法应该是使用fcntl:
int flags = fcntl(m_sock, F_GETFL, 0);
fcntl(m_sock, F_SETFL, flags|O_NONBLOCK);
这真是一个隐蔽的问题,折腾了我两天。线程每每停留在send()调用那里,我始终没怀疑到:用ioctl设置FIONBIO成功之后,socket竟然还是阻塞的。




A beautiful web dashboard for Linux
最近提交(Master分支:6 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
所有评论(0)