socket怎么设置为非阻塞模式

今天就跟大家聊聊有关socket怎么设置为非阻塞模式,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为南岳企业提供专业的做网站、成都网站建设,南岳网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

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;  
}

看完上述内容,你们对socket怎么设置为非阻塞模式有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。

分享名称:socket怎么设置为非阻塞模式
分享URL:https://www.cdcxhl.com/article22/ihgijc.html

成都网站建设公司_创新互联,为您提供网站制作自适应网站微信公众号商城网站小程序开发软件开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都seo排名网站优化