简体   繁体   中英

How to make WSAAsyncSelect start in an application console?

I've been trying to learn how to use sockets in C++ so i search for a tutorial i read it and the tutorial said that there were 2 ways to receive information from a socket, the 1st one is by making an infinite loop checking constantly recv and the other one was using asyncronous sockets, i tried using asyncronous but im getting the error 10022 i searched for the error and it says that there were an invalid argument in the function, i guess its the handle function i googled a way to find the way to solve this run-time error but i wasnt able to do it work.

public: static bool ListenOnPort(int port)
{
    int error = WSAStartup(0x0202, &w);
    if (error)
    {
        printf_s("Por alguna razón no se ha podido iniciar WSAStartup");
        WSACleanup();
        return false;
    }
    if (w.wVersion != 0x0202)
    {
        WSACleanup();

        return false;
    }
    SOCKADDR_IN addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    mysocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (mysocket == INVALID_SOCKET)
    {
        printf_s("No se ha podido iniciar el socket 's'");
        Functions::CloseConnection();
        return false;
    }
    listen(mysocket, SOMAXCONN);
    if (!WSAAsyncSelect(mysocket,Hwnd, MY_MESSAGE_NOTIFICATION, (FD_ACCEPT | FD_CONNECT |FD_READ | FD_CLOSE)))
    {
        printf("\nWSAAsyncSelect initialized succesfully\n");
    }
    else
    {
        printf("\nAn error ocurred while executing WSAsyngSelect (%d)[1]\n", WSAGetLastError());
    }
    if (bind(mysocket, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
    {
        printf("We coundnt bind");
        Functions::CloseConnection();
        return false;
    }
    return true;
}

And the WndProc function:

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    Hwnd = (HWND)GetModuleHandle(NULL);
    switch (message)
    {
        case MY_MESSAGE_NOTIFICATION:
        {
            switch (lParam)
            {
                case FD_ACCEPT:
                {
                    printf_s("There's a connection incomming\n");
                    accept(wParam, 0, 0);
                    break;
                }
                case FD_CONNECT:
                {
                    printf("A socket has just connected to the host.\n");
                    break;
                }
                case FD_READ:
                {
                    char buf[256];
                    memset(buf, 0, sizeof(buf));
                    recv(mysocket, buf, sizeof(buf)-1, 0);
                    break;
                }
                case FD_CLOSE:
                {
                    printf_s("A connection has just been shutted down.\n");
                    Functions::CloseConnection();
                    break;
                }
            }
        }
        default:return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
    //break;
}

Edit: An my main function:

int main(int argc, char *argv[])
{
    //printf_s("Ingresa la dirección IP para conectar.\n");
    if (argc != 0)
    {
        Functions::ListenOnPort(7182);
        while (true)
        {
            if (Conectado == false)
            {
                printf_s("\nConnecting to %s on port 7182\n", (char*)"127.0.0.1");
                Functions::ConnectToHost(7182, (char*)"127.0.0.1");
                cin.get();
            }
            else
            {
                Sleep(120);
            }
        }
    }
    Functions::CloseConnection();
    return 0;
}

This line of code is not right:

Hwnd = (HWND)GetModuleHandle(NULL);

A module handle is of type HMODULE and is something quite different from a window handle, of type HWND . You need the latter.

It looks as though you are not creating a window. You need to do that with a call to CreateWindow . You want to create a message only window by passing HWND_MESSAGE to the hWndParent parameter of CreateWindow . You must create the window before you call WSAAsyncSelect for the obvious reason that you need to have a window handle to pass to WSAAsyncSelect .

You will also need to run a message loop in order for your socket messages to be dispatched. That also seems to be missing from your program.

This is quite a complex subject. I suggest that you find some example code for async Windows sockets and start from there.

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