CAsyncSocket calls OnConnet(0) when the connection fails

I am porting an MFC application to PocketPC that uses the CAsyncSocket class. I was able to make it connect successfully to the server. However I have the following problem: If the server is not reachable or the server is not started, the OnConnect(int nErrorCode) is called with nErrorCode==0 (like on a successful connection). This is a problem because my internal state thinks it's connected, but obviously it isn't.
Has anyone experienced similar problems with the CAsyncSocket Is there some sample code available for a client CAsyncSocket application on the PocketPC That would help me to find the problem. The code works fine on the desktop version.

Here are some more details and some code snippets:
CTcpSocket is derived from CAsyncSocket, m_state is an internal state variable I need (mainly to keep track of the connection and to process the OnRecieve).
Connect returns WSAEWOULDBLOCK.
This seems fine however I would expect OnConnect to report an error.

void CTcpSocket::InitiateConnection()
{
if ( m_state == kNoSocket ) {
if ( Create() ) {
m_state = kSocketCreated;
DEBUG_OUTPUT("Socket created");
} else {
int err = GetLastError();
ReportWinsockError(err);
Close();
return;
}
}
if ( m_state == kSocketCreated ) {
if ( Connect(server, port) ) {
m_state = kConnected;
m_mainFrm->SetStatusBarMessage(3, "Connected");
} else {
int err = GetLastError();
if ( err == WSAEWOULDBLOCK ) {
m_state = kConnecting;
m_mainFrm->SetStatusBarMessage(3, "Connecting...");
} else {
ReportWinsockError(err);
if ( err == WSAENOTSOCK ) {
m_state = kNoSocket;
}
}
}
}
}

void CTcpSocket::OnConnect(int nErrorCode)

{
if ( nErrorCode == 0 || nErrorCode == WSAEISCONN ) {
m_mainFrm->SetStatusBarMessage(1, "Connected");
m_state = kConnected;
} else {
ReportWinsockError(nErrorCode);
if ( nErrorCode == WSAENOTSOCK ) {
m_state = kNoSocket;
} else {
m_state = kSocketCreated;
}
}
}


Answer this question

CAsyncSocket calls OnConnet(0) when the connection fails

  • GDigrego

    I'm using VS 2005 SP 1
    This problem occurs on the Windows Mobile 5 emulator and on the real device (iPAQ hx2415 upgraded from wm 2003 to wm5).

  • Blkbird

    While stepping through the MFC code with the debugger I found something that looks like a bug:
    UINT CAsyncSocket::CSocketInfo::WorkerThreadProc(LPVOID /*param*/)
    ....
    int iErrorCode = 0;
    ...
    if (events.lNetworkEvents & FD_CONNECT)
    {
    iErrorCode = events.iErrorCode[FD_CONNECT_BIT];
    ::PostMessage(hSocketWindow, WM_SOCKET_NOTIFY, (WPARAM)(SOCKET)hSocket, (LPARAM)FD_CONNECT);
    }
    At this stage iErrorCode has the value WSAECONNREFUSED like expected (the server is not running). However nothing is done with the local iErrorCode variable.

    When the message is handled later in the other thread
    void PASCAL CAsyncSocket::DoCallBack(WPARAM wParam, LPARAM lParam)
    it tries to get the error code with
    int nErrorCode = WSAGETSELECTERROR(lParam);
    (eg. HIWORD(lParam))

    However only FD_CONNECT was set in LPARAM, that explains why the error code is 0 instead of WSAECONNREFUSED.

    I know that CAsyncSocket has not the reputation of a nicely designed network class. However it would simplify the porting if I could reuse that class. Is anyone else using CAsyncSocket successfully on the PocketPC Or is it considered buggy and no one uses it

  • CAsyncSocket calls OnConnet(0) when the connection fails