WSAAsyncSelect freezes?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: WSAAsyncSelect freezes?

  1. #1
    Join Date
    Sep 2010
    Posts
    33

    WSAAsyncSelect freezes?

    Hello, I have a weird problem that i'm desperately trying to solve:

    I have a server which uses WSAAsyncSelect to handle multiple clients.
    WSAAsyncSelect sends notification messages to the main window's procedure and the main window's procedure sends them to another Thread which handles them.
    I can't post here the whole code because it's in too many files, and very long, but i'll post what's important.
    Here's the part in Thread that handles the messages:
    Code:
    while(GetMessage(&ThreadMsg, (HWND)-1, 0, 0))
    {
        if(ThreadMsg.message == TCPIPM_LISTENEVENT)
        {
    	 if(LOWORD(ThreadMsg.lParam) == FD_ACCEPT)
    	    cServer.Accept();
        }
        else
        {
            switch(LOWORD(ThreadMsg.lParam))
            {
    	    case FD_READ:
    		    char buffer[520];
                        recv(socket, buffer, 520, 0);
                        MessageBox(NULL, "Message!", 0, 0);
            }
        }
    }
    Now, it's not the whole code AT ALL, but it's the part that gives the problem, which is:
    if i send with the client multiple (4 times) send() one after the other, i receive only the first one.
    the problem is that MessageBox somehow "blocks" the thread after the first send() and the other 3 send() never received.
    if i eliminate MessageBox, the whole 4 send() are received correctly.
    WHY IS THAT???
    Please help me guys..

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553

    Re: WSAAsyncSelect freezes?

    A call to MessageBox() "steals" the message loop away from your application. During a call to MessageBox(), Windows instead runs its own message loop, which is the way that Windows makes the message box modal.

    The same thing happens whenever Windows is displaying a modal dialog. During the display of the modal dialog, Windows is running its own internal modal message loop, and your application's message loop is not allowed to execute.

    (Side note for MFC purists (not at all relevant to the OP's inquiry): all dialogs in MFC are mode-less. The MFC wrapper on dialogs that seem to be modal ensures that the behavior is similar to modal dialogs, even though in truth the dialog is actually mode-less.)

    Anyway, the short answer is never to use message boxes for debugging of applications that depend on a running message loop. Use something else instead. Try outputting to a debug console.

    Mike

  3. #3
    Join Date
    Sep 2010
    Posts
    33

    Re: WSAAsyncSelect freezes?

    thanks alot mike!
    but after MessageBox steals the loop, it does return to end the rest of the original loop (i checked it by writing another MessageBox after the first one), but for some reason the WSAAsycSelect stop sending FD_READ messages.
    i think it have something to do with this msdn info:
    Code:
    Here is a summary of events and conditions for each asynchronous notification message.
    
    FD_READ:
        2.When data arrives, if FD_READ is not already posted.
    another discovery:
    if i add WSAAsyncSelect (which ofcourse replace the original one) after MessageBox, it works! any idea why...??
    Last edited by tamirvs; October 21st, 2010 at 11:12 AM.

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553

    Re: WSAAsyncSelect freezes?

    Stop using MessageBox(). Period. It confuses all results. And even if you fully understand why some things work when MesageBox() is present, whereas some don't, it's all pointless anyway since your final code will not use MessageBox() either.

    Mike

  5. #5
    Join Date
    Sep 2010
    Posts
    33

    Re: WSAAsyncSelect freezes?

    yea you right.. i'll stop using it. thanks Mike!
    anyway, i'm gonna read a little about messagebox, just curious

  6. #6
    Join Date
    May 2007
    Posts
    11

    Re: WSAAsyncSelect freezes?

    It should work if you put the socket message handling code into the Window Procedure.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center