From: linguohua on
Hi,all.

My code pieces is:

m_SocketClient = WSASocket(AF_INET
,SOCK_STREAM,IPPROTO_TCP,NULL,0,WSA_FLAG_OVERLAPPED);

int retv = WSAEventSelect(m_SocketClient,m_hRefCommEvent,FD_CONNECT |
FD_READ | FD_WRITE | FD_CLOSE);

And I create a thread to wait for "m_hRefCommEvent", when event occur, it call
WSAEnumNetworkEvents() function:
int retv = WSAEnumNetworkEvents(m_SocketClient , m_hRefCommEvent,&events);

if "events" contains the FD_READ event, it post a message to the GUI thread
and GUI thread will call recv() to recieve data later.

According to the MSDN, the WSAEventSelect() function will put the socket in
non-blocking mode, so the recv() call will never block. But in fact, it block
at recv()
sometimes(when frequently start/stop the application).


I use Windbg to see the call stack of GUI thread , as follow:

0:ntdll!KiFastSystemCallRet
1:ntdll!NtWaitForSingleObject+0xc
2:mswsock!SockWaitForSingleObject+0x1a0
3:mswsock!WSPRecv+0x1dd
.......

The wait will never return, and the application hang.
It seem to Call SockWaitForSingleObject() to wait some event, e.g. an event
that the network driver will notify it. But the network driver will never
notify it,
and it hang.

Why this happend? Why non-blocking socket block at recv() call?

Can anybody help me?