In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to set socket to non-blocking mode. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
1. The socket created by either the socket () function or the WSASocket () function on the windows platform is in blocking mode:
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)
On the linux platform, when you create a socket with the socket () function, you can specify that the created socket is asynchronous:
Int socket (int domain, int type, int protocol)
You can set the SOCK_NONBLOCK flag in the parameters of type, for example:
Int s = socket (AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP)
two。 In addition, the socekt returned by the accept () function on windows and linux platforms is also blocked, and linux provides another accept4 () function that directly sets the returned socket to non-blocking mode:
Int accept (int sockfd, struct sockaddr * addr, socklen_t * addrlen); int accept4 (int sockfd, struct sockaddr * addr, socklen_t * addrlen, int flags)
Just set the last parameter flags of accept4 () to SOCK_NONBLOCK.
3. In addition to setting socket to non-blocking mode when creating a socket, you can also set it through the following API function:
The fcntl () or ioctl () function can be called on the linux platform. Examples are as follows:
Fcntl (sockfd, F_SETFL, fcntl (sockfd, F_GETFL, 0) | O_NONBLOCK); ioctl (sockfd, FIONBIO, 1); / / 1: non-blocking 0: blocking
Reference: http://blog.sina.com.cn/s/blog_9373fc760101i72a.html
However, there is also an article on the Internet (article link: http://blog.csdn.net/haoyu_linux/article/details/44306993) that if you call fcntl () to set socket to non-blocking mode under linux, you not only need to set the O_NONBLOCK mode, but also need to use the MSG_DONTWAIT flag when receiving and sending data, that is, when recv,recvfrom and send,sendto data, set flag to MSG_DONTWAIT. Whether it is necessary to carry out this double setting, the author does not think it is necessary. Because the description of the recv () function in the linux man manual about MSG_DONTWAIT is as follows:
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)).
Through this passage, I think either by setting the flags identity bit of the recv () function to MSG_DONTWAIT, or by setting the O_NONBLOCK identity through the fcntl () function, rather than setting it at the same time.
The ioctlsocket function can be called on windows:
Int ioctlsocket (_ In_ SOCKET s, _ In_ long cmd, _ Inout_ u_long * argp)
Set the cmd parameter to FIONBIO,*argp=0 to set to blocking mode, and * argp non-0 to set to non-blocking mode. But the windows platform needs to pay attention to one thing, if you call the WSAAsyncSelect () or WSAEventSelect () function on a socket, and then you call the ioctlsocket () function to set the socket to non-blocking mode, it will fail. You must first call WSAAsyncSelect () to disable WSAAsyncSelect () or WSAEventSelect () by setting the lEvent parameter to 0 or WSAEventSelect () to set the lNetworkEvents parameter to 0, respectively. Call ioctlsocket () again to set the socket to blocking mode. Because calling the WSAAsyncSelect () or WSAEventSelect () function automatically sets socket to non-blocking mode. The exact words on msdn are:
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.
Web site: https://msdn.microsoft.com/en-us/library/windows/desktop/ms738573(v=vs.85).aspx
4. In the code left by some of your predecessors in the actual project, the code for socket's non-blocking mode by calling the fcntl () or ioctlsocket () function in a loop is as follows:
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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.