Thread: Winsock asynchronous socket + MessageBox() = trouble?

  1. #1
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532

    Winsock asynchronous socket + MessageBox() = trouble?

    Hey, I've been writing a little chat server and I needed to do asynchronous sockets for it. So I set up the socket, binded it to the port and listened, then I passed the WSAAsyncSelect() function. Now, here is what my message-handler function looks like:
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
            switch(msg)
            {
                       case WM_SOCKETMESSAGE:   //my defined message(WM_USER+1)
                            if(WSAGETSELECTERROR(lParam))
                            {
                                 WSACleanup();
                                 return 0;
                            }
                            switch(WSAGETSELECTEVENT(lParam))
                            {
                                 case FD_CONNECT:
                                      clientSocket=accept(listeningSocket,NULL,NULL);
                                      if(clientSocket==INVALID_SOCKET)
                                      WSACleanup();
                                      else
                                      MessageBox(hwnd,"Connection established!","Incoming Connection",MB_OK);
                                      break;
                                 case FD_CLOSE:
                                      closesocket(clientSocket);
                                      break;
                                 case FD_READ:
                                      memset(buffer,0,500);
                                      recv(clientSocket,buffer,500,0);
                                      MessageBox(hwnd,buffer,"New Data Received",0);
                                      break;
                            }              
                            return 0;
    Now, I run the program and I CAN connect to it, so I know that it's working, however you see where I called the MessageBox function above, those never show up. I don't have a clue as to why it wouldn't execute that function or block it from showing up at all. Hmm... Any help is appreciated.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    If the HWND argument in the MessageBox() is not valid, it won't come up.
    That might not be it but I have encontered that before in the same way.
    Try calling DestroyWindow() on your HWND there and you'll know if it's the problem.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If the HWND argument in the MessageBox() is not valid, it won't come up.
    Obviously his HWND parameter is going to be valid since he uses the same value that was passed to his window procedure.

    jmd15 >> post the code where you call bind and WSAAsyncSelect(). Also, when you receive an error as a server, it is better to handle the error and continue receiving new connections, than to call WSACleanup().

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> Obviously his HWND parameter is going to be valid since he uses the same value that was passed to his window procedure.

    Your right, my mistake.

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yeah, the hwnd is valid as bithub explained, and MessageBox's do come up when used elsewhere, even within the message-handler function. This is my first attempt at asynchronous sockets and I'm doing it blindly so there are bound to be many mistakes in this code. Yes, bithub, writing an error-handling function is at the top of my TODO list .
    Code:
    #include <windows.h>
    #include <winsock.h>
    #define WM_SOCKETMESSAGE WM_USER+1
    
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    SOCKET listeningSocket;
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
    {
        HWND hwnd;
        MSG msg;
        WNDCLASS wc;
         
        wc.style=CS_HREDRAW|CS_VREDRAW;
        wc.cbWndExtra=0;
        wc.cbClsExtra=0;
        wc.hInstance=hInstance;
        wc.lpfnWndProc=WndProc;
        wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
        wc.hCursor=LoadCursor(NULL,IDC_ARROW);
        wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName=NULL;
        wc.lpszClassName="JCPPChat";
         
        if(!RegisterClass(&wc))
        return 0;
         
        hwnd=CreateWindow("JCPPChat","JCPPChat",WS_POPUPWINDOW|WS_CAPTION|WS_MINIMIZEBOX,(GetSystemMetrics(SM_CXSCREEN)/2)-320,(GetSystemMetrics(SM_CYSCREEN)/2)-240,640,480,NULL,NULL,hInstance,NULL);
        ShowWindow(hwnd,iCmdShow);
        UpdateWindow(hwnd);
         
        WSAData wsaData;
        WORD sockVer;
        int nret;
        
        sockVer=MAKEWORD(1,1);
        WSAStartup(sockVer,&wsaData);
        
        listeningSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        
        if(listeningSocket==INVALID_SOCKET)
        {
                                           WSACleanup();
                                           return 0;
        }
        SOCKADDR_IN serverInfo;
        serverInfo.sin_family=AF_INET;
        serverInfo.sin_addr.s_addr=INADDR_ANY;
        serverInfo.sin_port=htons(6783);
        nret=bind(listeningSocket,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
        if(nret==SOCKET_ERROR)
        {
                              WSACleanup();
                              return 0;
        }
        nret=listen(listeningSocket,1);
        if(nret==SOCKET_ERROR)
        {
                              WSACleanup();
                              return 0;
        }
        WSAAsyncSelect(listeningSocket,hwnd,WM_SOCKETMESSAGE,(FD_READ|FD_CONNECT|FD_CLOSE));
        while(GetMessage(&msg,NULL,0,0))
         {
                                         TranslateMessage(&msg);
                                         DispatchMessage(&msg);
         }
        closesocket(listeningSocket);
        WSACleanup();
        return msg.wParam;
    }
    SOCKET clientSocket;
    char buffer[500]={0};
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
            switch(msg)
            {
                       case WM_SOCKETMESSAGE:
                            if(WSAGETSELECTERROR(lParam))
                            {
                                 WSACleanup();
                                 return 0;
                            }
                            switch(WSAGETSELECTEVENT(lParam))
                            {
                                 case FD_CONNECT:
                                      clientSocket=accept(listeningSocket,NULL,NULL);
                                      if(clientSocket==INVALID_SOCKET)
                                      WSACleanup();
                                      else
                                      MessageBox(NULL,"We've accepted a new connection", "New Connection",0);
                                      break;
                                 case FD_CLOSE:
                                      closesocket(clientSocket);
                                      break;
                                 case FD_READ:
                                      memset(buffer,0,500);
                                      recv(clientSocket,buffer,500,0);
                                      MessageBox(hwnd,buffer,"New Data Received",0);
                                      break;
                            }              
                            return 0;
                            
                       case WM_DESTROY:
                            closesocket(clientSocket);
                            WSACleanup();
                            PostQuitMessage(0);
                            return 0;
            }
            return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    Sorry for that, I hate reading long bits of code too but not much I could have left out without confusing you. Thanks again for your replies.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    WSAData wsaData;
    This should be WSADATA

    You also need to pass FD_ACCEPT to WSAAsyncSelect().

  7. #7
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Ok, fixed those things. Should I be accepting the connection after receiving a FD_ACCEPT or an FD_CONNECT message? Is that the problem? Hmm... Thanks for those corrections bithub.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    call accept() when you get FD_ACCEPT.

  9. #9
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Thanks a lot bithub, that was exactly the problem. Now it works as I intended. Thanks again.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Asynchronous Socket Thread closes after first receive
    By AndreasS in forum C# Programming
    Replies: 1
    Last Post: 04-16-2009, 08:31 AM
  2. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  3. How to initialize a non blocking socket using only winsock library
    By *DEAD* in forum Networking/Device Communication
    Replies: 4
    Last Post: 01-18-2008, 07:03 AM
  4. Delete files that are used
    By Yuri in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2005, 01:48 PM
  5. Odd socket trouble
    By Twiggy in forum C Programming
    Replies: 0
    Last Post: 03-31-2003, 05:25 PM