简体   繁体   中英

Asynchronous Winsock Server's WndProc not being called

I'm trying to learn to make a proper Winsock server, and got the below code from some trial and effort with examples found online. My server does show up in 'netstat -an', and can be connected to via the Hercules IO debugger application. Unfortunately, none of the Message Boxes seen in the WndProc ever show up.

Sockets.h:

#include <winsock.h>

#pragma comment(lib, "ws2_32.lib")

SOCKET s;
WSADATA w;
#define MY_MESSAGE_NOTIFICATION      1048

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    MessageBox(hwnd, (LPCWSTR)L"Test1", (LPCWSTR)L"Test1", MB_OK);
    switch (message)
    {
    case MY_MESSAGE_NOTIFICATION:
        {
            switch (lParam)
            {
            case FD_ACCEPT:
                break;
            case FD_CONNECT:
                MessageBox(hwnd,(LPCWSTR)L"Test2",(LPCWSTR)L"Test2",MB_OK);
                break;
            case FD_READ:
                char buffer[80];
                memset(buffer, 0, sizeof(buffer)); 
                recv (s, buffer, sizeof(buffer)-1, 0); 
                MessageBox(hwnd, (LPCWSTR)buffer, (LPCWSTR)L"Captured Text…", MB_OK);
                break;
            case FD_CLOSE:
                break;
            }
        }
        break;
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
}

int ListenOnPort(int portno)
{
    int error = WSAStartup (0x0202, &w);
    if (error)
    {
        return false;
    }
    if (w.wVersion != 0x0202)
    {
        WSACleanup ();
        return false;
    }
    SOCKADDR_IN addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons (portno);
    addr.sin_addr.s_addr = htonl (INADDR_ANY);  
    s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (s == INVALID_SOCKET)
    {
        return false;
    }
    if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
    {
        return false;
    }
    listen(s, SOMAXCONN);
    WSAAsyncSelect (s, GetConsoleWindow(), MY_MESSAGE_NOTIFICATION, (FD_ACCEPT | FD_CONNECT |
         FD_READ | FD_CLOSE));
}

You are telling WSAAsyncSelect() to send messages to an HWND that you do not own. Unless you have manually subclassed that window and hooked up WndProc() to it, WndProc() will never receive the socket messages. You need to create your own HWND instead. And make sure you also have a message loop in your code, or your HWND will never receive messages.

Why don't you just check the return value of WSAAsyncSelect, it should tell you what's wrong, it seems that it doesn't even register on the window.

Generally you should avoid using the console window as a HWND ( GetConsoleWindow() ) since it is a special window handled by the system and thus it may behave differnetly. Also what calls that WndProc function? by the looks of it nothing since you use the console's window which has its own WndProc!

So just try to make a window for yourself and specify that in the WSAAsyncSelect call and make it invisible so it wont bother the user at all and when you're done just destroy the window!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM