欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

服務器編程心得(四)——如何將socket設置為非阻塞模式-創(chuàng)新互聯(lián)

  1. windows平臺上無論利用socket()函數(shù)還是WSASocket()函數(shù)創(chuàng)建的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
);

網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、成都小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了靖西免費建站歡迎大家使用!

linux平臺上可以在利用socket()函數(shù)創(chuàng)建socket時指定創(chuàng)建的socket是異步的:

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

在type的參數(shù)中設置SOCK_NONBLOCK標志即可,例如:

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

2. 另外,windows和linux平臺上accept()函數(shù)返回的socekt也是阻塞的,linux另外提供了一個accept4()函數(shù),可以直接將返回的socket設置為非阻塞模式:

int accept(int sockfd, struct sockaddr addr, socklen_taddrlen);

int accept4(int sockfd, struct sockaddr addr, socklen_taddrlen, int flags);

只要將accept4()最后一個參數(shù)flags設置成SOCK_NONBLOCK即可。

3. 除了創(chuàng)建socket時,將socket設置成非阻塞模式,還可以通過以下API函數(shù)來設置:

linux平臺上可以調(diào)用fcntl()或者ioctl()函數(shù),實例如下:

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

但是網(wǎng)上也有文章說(文章鏈接:http://blog.csdn.net/haoyu_linux/article/details/44306993),
linux下如果調(diào)用fcntl()設置socket為非阻塞模式,不僅要設置O_NONBLOCK模式,還需要在接收和發(fā)送數(shù)據(jù)時,需要使用MSG_DONTWAIT標志,即在recv,recvfrom和send,sendto數(shù)據(jù)時,將flag設置為MSG_DONTWAIT。是否有要進行這種雙重設定的必要,筆者覺得沒有這個必要。因為linux man手冊上recv()函數(shù)的說明中關于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()函數(shù)的flags標識位為MSG_DONTWAIT,要么通過fcntl()函數(shù)設置O_NONBLOCK標識,而不是要同時設定。

windows上可調(diào)用ioctlsocket函數(shù):

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

將cmd參數(shù)設置為 FIONBIO,*argp=0即設置成阻塞模式,而*argp非0即可設置成非阻塞模式。但是windows平臺需要注意一個地方,如果你對一個socket調(diào)用了WSAAsyncSelect()或WSAEventSelect()函數(shù)后,你再調(diào)用ioctlsocket()函數(shù)將該socket設置為非阻塞模式,則會失敗,你必須先調(diào)用WSAAsyncSelect()通過設置lEvent參數(shù)為0或調(diào)用WSAEventSelect()通過設置lNetworkEvents參數(shù)為0來分別禁用WSAAsyncSelect()或WSAEventSelect()。再次調(diào)用ioctlsocket()將該socket設置成阻塞模式才會成功。因為調(diào)用WSAAsyncSelect()或WSAEventSelect()函數(shù)會自動將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.

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

4. 在看實際項目中以前一些前輩留下來的代碼中,通過在一個循環(huán)里面調(diào)用fcntl()或者ioctlsocket()函數(shù)來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;
}


是否有必要這樣做,有待考證。

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網(wǎng)頁標題:服務器編程心得(四)——如何將socket設置為非阻塞模式-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://chinadenli.net/article30/dephso.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站網(wǎng)頁設計公司、微信小程序、商城網(wǎng)站、微信公眾號定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站