服务器编程心得(四)—— 如何将socket设置为非阻塞模式
admin
2023-03-10 20:41:06
0
  1. windows平台上无论利用socket()函数还是WSASocket()函数创建的socket都是阻塞模式的:
    
    SOCKET WSAAPI socket(
    _In_ int af,
    _In_ int type,
    _In_ int protocol
    );

SOCKET WSASocket(
In int af,
In int type,
In int protocol,
In LPWSAPROTOCOL_INFO lpProtocolInfo,
In GROUP g,
In DWORD dwFlags
);


linux平台上可以在利用socket()函数创建socket时指定创建的socket是异步的:

int socket(int domain, int type, int protocol);

在type的参数中设置SOCK_NONBLOCK标志即可,例如:

int s = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);

2. 另外,windows和linux平台上accept()函数返回的socekt也是阻塞的,linux另外提供了一个accept4()函数,可以直接将返回的socket设置为非阻塞模式:

int accept(int sockfd, struct sockaddr addr, socklen_t addrlen);

int accept4(int sockfd, struct sockaddr addr, socklen_t addrlen, int flags);

只要将accept4()最后一个参数flags设置成SOCK_NONBLOCK即可。

3. 除了创建socket时,将socket设置成非阻塞模式,还可以通过以下API函数来设置:

linux平台上可以调用fcntl()或者ioctl()函数,实例如下:

fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK);

ioctl(sockfd, FIONBIO, 1); //1:非阻塞 0:阻塞


参考: http://blog.sina.com.cn/s/blog_9373fc760101i72a.html

但是网上也有文章说(文章链接:http://blog.csdn.net/haoyu_linux/article/details/44306993),
linux下如果调用fcntl()设置socket为非阻塞模式,不仅要设置O_NONBLOCK模式,还需要在接收和发送数据时,需要使用MSG_DONTWAIT标志,即在recv,recvfrom和send,sendto数据时,将flag设置为MSG_DONTWAIT。是否有要进行这种双重设定的必要,笔者觉得没有这个必要。因为linux man手册上recv()函数的说明中关于MSG_DONTWAIT说明如下:

Enables nonblocking operation; if the operation would block, the call fails with the error EAGAIN or EWOULDBLOCK (this can also be enabled using the O_NONBLOCK flag  with the F_SETFL fcntl(2)).

通过这段话我觉得要么通过设置recv()函数的flags标识位为MSG_DONTWAIT,要么通过fcntl()函数设置O_NONBLOCK标识,而不是要同时设定。

windows上可调用ioctlsocket函数:

int ioctlsocket(
In SOCKET s,
In long cmd,
Inout u_long *argp
);

将cmd参数设置为 FIONBIO,*argp=0即设置成阻塞模式,而*argp非0即可设置成非阻塞模式。但是windows平台需要注意一个地方,如果你对一个socket调用了WSAAsyncSelect()或WSAEventSelect()函数后,你再调用ioctlsocket()函数将该socket设置为非阻塞模式,则会失败,你必须先调用WSAAsyncSelect()通过设置lEvent参数为0或调用WSAEventSelect()通过设置lNetworkEvents参数为0来分别禁用WSAAsyncSelect()或WSAEventSelect()。再次调用ioctlsocket()将该socket设置成阻塞模式才会成功。因为调用WSAAsyncSelect()或WSAEventSelect()函数会自动将socket设置成非阻塞模式。msdn上的原话是:

The WSAAsyncSelect and WSAEventSelect functions automatically set a socket to nonblocking mode. If WSAAsyncSelect or WSAEventSelect has been issued on a socket, then any attempt to use ioctlsocket to set the socket back to blocking mode will fail with WSAEINVAL.

To set the socket back to blocking mode, an application must first disable WSAAsyncSelect by calling WSAAsyncSelect with the lEvent parameter equal to zero, or disable WSAEventSelect by calling WSAEventSelect with the lNetworkEvents parameter equal to zero.

网址:https://msdn.microsoft.com/en-us/library/windows/desktop/ms738573(v=vs.85).aspx

4. 在看实际项目中以前一些前辈留下来的代码中,通过在一个循环里面调用fcntl()或者ioctlsocket()函数来socket的非阻塞模式的,代码如下:

for (;;)
{
#ifdef UNIX
on=1;
if (ioctlsocket(id, FIONBIO, (char *)&on) < 0)
#endif

#ifdef WIN32
unsigned long on_windows=1;
if (ioctlsocket(id, FIONBIO, &on_windows) < 0)
#endif

#ifdef VOS
int off=0;
if (ioctlsocket(id, FIONBIO, (char *)&off) <0)
#endif
{
if (GET_LAST_SOCK_ERROR() == EINTR)
continue;
RAISE_RUNTIME_ERROR("Can not set FIONBIO for socket");
closesocket(id);
return NULL;
}
break;
}


是否有必要这样做,有待考证。

相关内容

热门资讯

邮轮疫情悄然来袭 汉坦病毒溯源... 参考消息网5月8日报道美国《科学新闻》双周刊网站5月5日刊登题为《关于海上暴发的罕见汉坦病毒疫情,需...
打破“一边欢迎批评,一边屏蔽评... 澎湃首席评论员 与归因为母亲节宣传文案引起争议,5月8日下午,OPPO官方微博发文道歉,表示“已第一...
盘点闺蜜机哪个牌子耐用,适合闺... 闺蜜机哪个牌子耐用适合闺蜜同用?高性价比之选认准深圳市翰视科技有限公司 闺蜜机凭借可移动、大尺寸、...
“本源悟空-180”来了 我国... 记者今天(9日)从安徽省量子计算芯片重点实验室获悉,搭载单核180个计算比特自主超导量子芯片的“本源...
机械振动故障诊断实践总结 通常,振动频率用于确定机器中故障的位置。 故障诊断主要在频谱中进行;然而,时间波形、轴心轨迹和相位分...
比亚迪、特斯拉、小鹏、理想、蔚... 作者 | 黄琳 肖逸思 葛慧 武子晔近日,有市场消息称,车企锁电引起大范围投诉事宜引起监管重视,8家...
伊朗称美军打击伊朗6艘民用船 新华社德黑兰5月9日电 据伊朗迈赫尔通讯社9日报道,伊朗伦格港地方官员法瓦德·穆拉德扎德表示,8日晚...
浙江人形与杰克科技签约2000... 来源:上海证券报·中国证券网 上证报中国证券网讯(记者 王子霖)据浙江人形机器人创新中心有限公司(以...
原创 三... 不出意外的话,今年苹果,高通、三星,联发科,这4家确定会推出2nm的手机芯片。 并且高通、联发科、苹...
Kimi、阶跃再获百亿融资,D... 图片由AI生成 出品 | 搜狐科技 作者 | 梁昌均 编辑 | 杨锦 中国大模型创业公司再次迎来融资...