标签:style io ar strong sp art on amp ad
windows的socket在创建后,默认是阻塞调用的,也就是说函数recv,recvfrom,send,sendto等函数都是阻塞的;那么我们如何将他们设置成非阻塞调用呢?我们可以通过windows为我们提供的ioctlsocket 函数实现;先给出一个例子:
BOOL LoadSocketSystem(void) { <span style="white-space:pre"> </span>WORD wVersionRequested; <span style="white-space:pre"> </span>SOCKADDR_IN addrSrv; <span style="white-space:pre"> </span>BOOL bRet = FALSE; <span style="white-space:pre"> </span>WSADATA wsaData; <span style="white-space:pre"> </span>SOCKET sockClient; <span style="white-space:pre"> </span>int err; <span style="white-space:pre"> </span>int iMode; <span style="white-space:pre"> </span>wVersionRequested = MAKEWORD( 1, 1 ); <span style="white-space:pre"> </span>err = WSAStartup( wVersionRequested, &wsaData ); <span style="white-space:pre"> </span>if ( err != 0 ) { <span style="white-space:pre"> </span>return bRet; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { <span style="white-space:pre"> </span>WSACleanup( ); <span style="white-space:pre"> </span>return bRet; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>s_socketStatus.m_hsocket = socket(AF_INET,SOCK_STREAM,0); <span style="white-space:pre"> </span>if( INVALID_SOCKET == s_socketStatus.m_hsocket ) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>WSACleanup( ); <span style="white-space:pre"> </span>return bRet; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>iMode = 1; <span style="white-space:pre"> </span>ioctlsocket(s_socketStatus.m_hsocket,FIONBIO,(u_long FAR*) &iMode); <span style="white-space:pre"> </span>s_socketStatus.m_isConnected = TRUE; <span style="white-space:pre"> </span>sockClient = s_socketStatus.m_hsocket; <span style="white-space:pre"> </span>addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); <span style="white-space:pre"> </span>addrSrv.sin_family=AF_INET; <span style="white-space:pre"> </span>addrSrv.sin_port=htons(6000); <span style="white-space:pre"> </span>connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); <span style="white-space:pre"> </span>return TRUE; }上面红色表示的就是将刚才创建的socket设置成非阻塞操作。
当然我们这样做是最简单的,但是如果对于一个复杂的系统,系统的其他部分可能已经对该socket调用了另两个windows函数--WSAAsyncSelector WSAEventSelect,这个个函数都是自动的将socket设置成非阻塞形式,所有在这样的情况下你调用ioctlsocket 想将其设置回阻塞形式是不会成功的;要想成功,你必须将由于调用上面两个函数对socket系统产生的影响clear掉,然后才能成功调用ioctlsocket 函数;如果clear呢?
如下调用WSAEventSelect函数:
rc = WSAEventSelect(s, hEventObject, 0);
如果想详细了解上述几个函数,请参阅MSDN。
标签:style io ar strong sp art on amp ad
原文地址:http://blog.csdn.net/tiandesheng111/article/details/40208961