What is behind CAsyncSocket approval issues and "wrong argument" errors in my MFC socket code?

I was asked to look at some code for a friend. (I rightfully hesitated due to MFC and a lot of bad code, but it won ...)

This is a dialog based application that uses CAsyncSocket.

The problem manifests itself in some non-stop debugbreaks and other similar things - there is also a problem with the MFC macro ENSURE()- checking the socket for zero. All problems occur in the MFC.

Potential resource leaks have appeared on some search engines if you use themes in Vista / XP, but I don’t think this is a problem.

The code is pretty bad, based on my couple of debugging hours, but basically it does the following:

(When the connection is established, there is no problem - this is only the case when there is no connection)

  • Connect call (server, socket) (on a derived object CAsyncSocket)
  • They OnConnect()tell us that the connection is not working / not connected.
  • Inside the window timer for the main dialog / application there is a timer. When the event / timer handler is called, we check if it is connected.
  • If we find that we are not connected ( OnConnect()does not fit), then we call CAsyncSocket::Close(), then call CAsyncSocket::Create()(without parameters), then callCAsyncSocket::Connect(server, port)

Note that the initial call Connect()did not have a previous call Create().

My first real question is:

  • What is the difference between the two and why is it needed Create()? (if I delete this, then it no longer fails, but I also do not connect when I reconnect)

:

  • ?
  • ?

EDIT:

, Create(), Connect().

CAsyncSocket::DoCallBack() - :

void PASCAL CAsyncSocket::DoCallBack(WPARAM wParam, LPARAM lParam)
{
    if (wParam == 0 && lParam == 0)
        return;

    // Has the socket be closed - lookup in dead handle list
    CAsyncSocket* pSocket = CAsyncSocket::LookupHandle((SOCKET)wParam, TRUE);

    // If yes ignore message
    if (pSocket != NULL)
        return;

    pSocket = CAsyncSocket::LookupHandle((SOCKET)wParam, FALSE);
    if (pSocket == NULL)
    {
        // Must be in the middle of an Accept call
        pSocket = CAsyncSocket::LookupHandle(INVALID_SOCKET, FALSE);
        ENSURE(pSocket != NULL);

, : " "

( ), MFC , . (DoCallback()), Close() .

, MFC, .

+5
2

. , , .

Microsoft ( , ). , .

, API , , .

, , MFC, "AHA" . Winsock - UNIX, - , /, , .

CAsyncSocket, , , MFC ( ), Windows . [, , , Win32 ].


Update:

, , , , . http://msdn.microsoft.com/en-us/library/3d46645f(VS.80).aspx,

CAsyncSocket, , Create ... - Connect.

, , , , Windows , .

UNIXy ( ), ( ).

, , , WinSock (, Windows 3.11, ) [ , API UNIX , , select() /].


:

( ) , / , . , , .

, , .

MFC, , . ( ), ( ) - , OnConnect().

create() , , (.. OnXXX()). Win32 GUI, ( ). , - , , "", .

, CHATSRVR, , .

+6

, , , , MFC. , : " MFC, . ?".

, , , . , ( ) .

+4

All Articles