WSAEnumNetworkEvents reports successful FD_CONNECT after call to connect() fails RRS feed

  • Question

  • I have a simple Windows Mobile 6.5 application that uses the Winsock connect API.  It uses WSAEventSelect to receive connect notification, and when it receives notification, it calls WSAEnumNetworkEvents to get the results.  When I run the application, I have the following situation:

    1) I sign up with WSAEventSelect for FD_CONNECT notification using my ConnectEvent.

    2) I call connect on Thread1.  The call fails with SOCKET_ERROR and WSAGetLastError returns WSAECONNREFUSED.

    3) Thread2 wakes up because ConnectEvent is signaled.  I call WSAEnumNetworkEvents, and it returns success.  The lNetworkEvents field of the WSANETWORKEVENTS indicates the FD_CONNECT network event, but iErrorCode[FD_CONNECT_BIT] is set to 0.

    I was hoping to do error processing on Thread2, and expected to find the error information in WSANETWORKEVENTS.iErrorCode.  Was my expectation wrong?

    Thanks in advance for any help.

    Friday, September 23, 2011 12:41 AM

All replies

  • how about WSAGetLastError() return value?
    Monday, September 26, 2011 9:36 AM
  • On Thread1 after the failed call to connect(),  WSAGetLastError() returns WSAECONNREFUSED.  On Thread2 after the ConnectEvent is signalled, it returns 0.
    Monday, September 26, 2011 4:19 PM
  • Hello,


    I hope you can provide some codes to us, you can post codes here, also you can upload your project to skydriver.


    It is not necessary that you send out the whole of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business details from it.


    Best regards,


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 29, 2011 3:14 AM
  • Hi Jesse,

    Here's some simplified code that causes the behavior I described.  The behavior occurred on a PocketPC device running Windows Mobile 5.0 and also on a smartphone running Windows Mobile 6.5.  It happens consistently with the device connected to my network via Active Sync.  I couldn't get it to happen with the device connected via wireless.

    (***Note you should replace the "xxx.xxx.x.xxx" in the code with an applicable IP address.***)

    Thanks for your attention to this.

    #include "stdafx.h"
    #include <windows.h>
    #include <commctrl.h>
    #include <winsock2.h>
    #include <string>
    // Arguments passed to worker thread 
    struct ThreadParams
    	HANDLE hEvent;
    	SOCKET hSocket;
    // Helper to display messages for our console app
    void DisplayMessage(const wchar_t* msg, DWORD err=0)
    	if (err)
    		wchar_t buff[1024] = { 0 };
    		wsprintf(buff, msg, err);
    		MessageBox(NULL, buff, L"SocketConnect", MB_OK);
    		MessageBox(NULL, msg, L"SocketConnect", MB_OK);
    // Simple exception class for easier resource cleanup
    class CSimpleException
    	CSimpleException(const wchar_t* msg, DWORD err) 
    		: ws(msg), dwError(err)
    	void DisplayMsg()
    		DisplayMessage(ws.c_str(), dwError);
    	std::wstring ws;
    	DWORD dwError;
    // Starting point for the worker thread
    UINT _stdcall ThreadFunc( LPVOID pv )
    	ThreadParams *pParams = (ThreadParams*)pv;
    	DWORD dwStat = WaitForSingleObject(pParams->hEvent, INFINITE);
    	if (dwStat != WAIT_OBJECT_0)
    		DisplayMessage(L"ThreadFunc: WaitForSingleObject failed: %d", GetLastError());
    		ZeroMemory(&wsaEvents, sizeof(wsaEvents));
    		int iStat = WSAEnumNetworkEvents(pParams->hSocket, 0, &wsaEvents);
    		if (iStat == 0) // call succeeded
    			if (wsaEvents.lNetworkEvents & FD_CONNECT)
    				if (wsaEvents.iErrorCode[FD_CONNECT_BIT] == 0) //connected
    					DisplayMessage(L"FD_CONNECT event signaled with no 
    					DisplayMessage(L"FD_CONNECT event signaled with error: 
    				DisplayMessage(L"ThreadFunc received unexpected network event 
    			DisplayMessage(L"WSAEnumNetworkEvents failed with error: %d\n", 
    	return 0;
    int _tmain(int argc, _TCHAR* argv[])
    	SOCKET hSocket = 0;
    	HANDLE hEvent = 0;
    	HANDLE hThread = 0;
    		// Initialize Winsock
    		WSADATA wsaData;
    		int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    		if (iResult != NO_ERROR)
    			throw CSimpleException(L"WSAStartup function failed with error: %d\n", 
    		// Create a SOCKET for connecting to server
    		hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    		if (hSocket == INVALID_SOCKET)
    			throw CSimpleException(L"socket function failed with error: %d\n", 
    		// The sockaddr_in structure specifies the address family,
    		// IP address, and port of the server to be connected to.
    		sockaddr_in remoteSockAddr;
    		remoteSockAddr.sin_family = AF_INET;
    		// ** specify a reachable address at which there is no
    		// socket listening at the specified port **
    		remoteSockAddr.sin_addr.s_addr = inet_addr("xxx.xxx.x.xxx");
    		remoteSockAddr.sin_port = htons(50000);
    		// Set up connect event notification
    		hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    		if (hEvent == NULL)
    			throw CSimpleException(L"CreateEvent function failed with error: %d\n", 
    		DWORD dwStat = WSAEventSelect(hSocket, hEvent, FD_CONNECT);
    		// Create the thread that will wait for the connect event
    		ThreadParams threadParams;
    		threadParams.hEvent = hEvent;
    		threadParams.hSocket = hSocket;
    		DWORD dwThreadId = 0;
    		DWORD stack_size = 1024*128;
    		hThread =  CreateThread(
    		if (hThread == NULL)
    			throw CSimpleException(L"CreateThread function failed with error: %d\n", 
    		// Connect to server.
    		iResult = connect(hSocket, (PSOCKADDR)&remoteSockAddr, sizeof(remoteSockAddr));
    		if (iResult == SOCKET_ERROR)
    			throw CSimpleException(L"connect function failed with error: %d\n", 
    		// Wait for worker thread to exit.
    		WaitForSingleObject(hThread, INFINITE);
    	catch(CSimpleException &ex)
    	if (hSocket)
    	if (hThread)
    	if (hEvent)
    	return 0;

    Monday, October 3, 2011 6:19 PM
  • try to use WSA-functions suck as WSAWaitFor.., WSACreateEvent etc
    Wednesday, October 5, 2011 2:38 PM
  • try to use WSA-functions suck as WSAWaitFor.., WSACreateEvent etc

    Thanks for your suggestion.  Before I try it out, can you explain why using the WSA functions would make a difference?  I thought it was okay to use the non-WSA versions of the functions -- for instance, the WSACreateEvent documentation says that an application that requires an auto-reset event may use CreateEvent instead.
    Wednesday, October 5, 2011 9:37 PM
  • I was hoping to arrive at an answer to my original question with some help from this Windows Mobile forum, but despite the replies so far (which I appreciate), haven't found an answer yet.  Can anyone suggest some alternate resources for finding some help?
    Saturday, October 22, 2011 11:40 PM