CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2009
    Location
    Poland
    Posts
    3

    Exclamation CAsyncSocket OnConnect event strange behaviour

    Hello,

    I've got a MFC application using a custom class derived from CAsyncSocket class. Let's say that the name of my class is "MyClass". Then I've got the following code in my MFC application:

    ...
    AfxSocketInit();
    MyClass socket;
    socket.Create();
    if(socket.Connect(_T("localhost"), 80)) // marked line - talking about it later
    {
    MessageBox("Connected");
    }
    ...

    Obviously the "Connected" message never appears, as it takes some time for the socket to connect. So I have an overridden OnConnect handler, which should throw its own MessageBox. But it doesn't either. The funny thing is that if I change the marked line from
    if(socket.Connect(_T("localhost"), 80))
    to
    if(!socket.Connect(_T("localhost"), 80)) // (note the "!" at the begining of the condition)
    then I get both the "Connected" message from the condition (a false connect though) AND THE MESSAGE FROM MY OVERRIDDEN OnConnect handler!

    To sum up, I can only get the OnConnect event handler invoked if I display any MessageBox just after calling the Connect() function. Otherwise it doesn't work.

    Do you have any ideas how can I have the OnConnect handler still working without having to display the MessageBox each time I call the Connect method?

    Thanks in advance.
    Maciej, Poland.

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: CAsyncSocket OnConnect event strange behaviour

    Well, I haven't seen your OnConnect function, but I can say this is a regular behaviour, not strange
    After connect finishes it's work (succesfully or not) OnConnect is always called (with or without the MessageBox after the connect calling).

    I think it might be helpful:
    http://msdn.microsoft.com/en-us/libr...kz(VS.80).aspx
    http://msdn.microsoft.com/en-us/libr...xe(VS.80).aspx

  3. #3
    Join Date
    Oct 2009
    Location
    Poland
    Posts
    3

    Exclamation Re: CAsyncSocket OnConnect event strange behaviour

    Thank you very much for the response. However, I see that I haven't explained the problem well enough, as the OnConnect handler is, in my case, NOT ALWAYS called, which I find strange. Thus, I'm posting the exact example - one working, and the other one, surprisingly, not.

    My custom OnConnect funciton:

    void MyClass:OnConnect(int nErrorCode)
    {
    (*messageBoxHandler)("OnConnect handler called");
    // the above calls a function throwing a MessageBox through a pointer
    }

    This part of code remains constant all the time. Now I'm presenting two very similar versions of the connection process. One working (the OnConnect handler is called), one not.


    NOT WORKING VERSION:
    ...
    AfxSocketInit();
    MyClass socket;
    socket.Create();
    socket.Connect(_T("localhost"), 80);
    ...

    Output:
    The OnConnect function is never called and the Message box doesn't appear

    WORKING VERSION:
    ...
    AfxSocketInit();
    MyClass socket;
    socket.Create();
    socket.Connect(_T("localhost"), 80);
    MessageBox("Any message box after Connect() makes it working");
    ...
    Output:
    1. MessageBox: "OnConnect handler called"
    2. MessageBox: "Any message box after Connect() makes it working"


    To sum up (once again ), any MessageBox call after calling the socket.Connect() function makes the OnConnect event be called later too. Deleting the last line of the working version - the MessageBox("Any...") line, makes the OnConnect event never be called.

    I hope the situation is clear now, even with my not-so-perfect English
    Thank you in advance,
    Maciej.

  4. #4
    Join Date
    Dec 2008
    Posts
    144

    Re: CAsyncSocket OnConnect event strange behaviour

    Ok. It's clear. I just haven't understood you at first.
    The problem is your MyClass socket; is a local variable. And it is destroyed every time the function finishes.
    Meanwhile the OnConnect function is called as a respond for the corresponding WM_SOCKET_NOTIFY message. So, in the first case, when you application can start to handle this message your socket object is already destroyed.
    You can declare this "MyClass socket" as a member of some class or create it dynamicly to solve this problem.

  5. #5
    Join Date
    Oct 2009
    Location
    Poland
    Posts
    3

    Resolved Re: CAsyncSocket OnConnect event strange behaviour

    Thank you very much, now it's clear for me too.
    Regards, Maciej.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured