如何使WSAAsyncSelect在应用程序控制台中启动?

时间:2014-05-08 21:33:46

标签: c++ sockets winapi

我一直在努力学习如何在C ++中使用套接字,所以我搜索了一个教程,我读了它,教程说有两种方法从套接字接收信息,第一种是通过无限循环检查不断recv和另一个使用异步套接字,我尝试使用asyncronous但我得到错误10022我搜索错误,它说在函数中有一个无效的参数,我想它的句柄功能我googled一种方式找到解决此运行时错误的方法,但我无法正常工作。

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;
}

WndProc功能:

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;
}

编辑:我的主要功能:

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;
}

1 个答案:

答案 0 :(得分:3)

这行代码不正确:

Hwnd = (HWND)GetModuleHandle(NULL);

模块句柄的类型为HMODULE,与HWND类型的窗口句柄完全不同。你需要后者。

看起来好像你没有创建一个窗口。您需要通过拨打CreateWindow来执行此操作。您希望通过将HWND_MESSAGE传递到hWndParent CreateWindow参数来创建仅限消息的窗口。您必须在致电WSAAsyncSelect之前创建窗口,原因很明显,您需要将窗口句柄传递给WSAAsyncSelect

您还需要运行消息循环,以便分派您的套接字消息。您的计划似乎也缺少这一点。

这是一个非常复杂的主题。我建议您找到一些异步Windows套接字的示例代码,然后从那里开始。