0

这是 Main.cpp 直到发生错误的地方:

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
    if(SUCCEEDED(CoInitialize(NULL)))
    {
        {
            Game game;

            game.CreateRessources(hInst);

            game.ShowMainScreen();

            game.pWinsock->Initialize(game.Getm_hWnd());

game.Getm_hWnd 返回私有 HWND m_hWnd。

这是游戏::CreateRessources(HINSTANCE):

void Game::CreateRessources(HINSTANCE hInst)
{   
    m_hWnd=CreateWindowClass(hInst);

    pMessageLog=CreateMessageLog();
    pD2DResources=CreateD2DResources(m_hWnd);
    pWinsock=CreateWinsock();

}

有 CreateWinsock():

Winsock* CreateWinsock()
{
    Winsock* pWinsock=new Winsock;

    return pWinsock;
}

温索克::温索克:

Winsock::Winsock() : 套接字(NULL) { }

最后,Winsock::Initialize(HWND):

void Winsock::Initialize(HWND hwnd)
{
    WSADATA wsaDat;

    SendMessage(hwnd, LOG_ADD, NULL, (LPARAM)L"Initializing winsock... ");

    int nResult = WSAStartup(MAKEWORD(2,2),&wsaDat);
    if(nResult!=0)
    {
        MessageBox(NULL,"Winsock initialization failed","Critical error",MB_ICONERROR);
        SendMessage(hwnd,WM_DESTROY,NULL,NULL);
    }

    SendMessage(hwnd, LOG_ADD, NULL, (LPARAM)L"Done!\nCreating a socket... ");

    Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(Socket==INVALID_SOCKET)
    {
        MessageBox(NULL,"Socket Creation failed","Critical error",MB_ICONERROR);
        SendMessage(hwnd,WM_DESTROY,NULL,NULL);
    }

    SendMessage(hwnd, LOG_ADD, NULL, (LPARAM)L"Done!\nRequesting Windows message-based notification of network events... ");

    nResult=WSAAsyncSelect(Socket,hwnd,WM_SOCKET,(FD_CLOSE|FD_READ));
    if(nResult)
    {
        if(WSAGetLastError()==WSAENOTSOCK)
            MessageBox(hwnd,"WSAENOTSOCK Error!","Error",NULL);
        MessageBox(NULL,"WSAAsyncSelect failed","Critical error",MB_ICONERROR);
        SendMessage(hwnd,WM_DESTROY,NULL,NULL);
    }

    /* More code */
}

if(WSAGetLastError()==WSAENOTSOCK) 行返回 true。WSAENOTSOCK 的含义如下:

“非套接字上的套接字操作。尝试对非套接字进行操作。套接字句柄参数未引用有效套接字,或者对于 select,fd_set 的成员无效。”

编辑:有我的 Winsock 课程:

class Winsock{
public:
    Winsock();

    void Initialize(HWND);

    void ReceiveMsg();

private:
    SOCKET Socket;
    static const char* server;
    static const int port;
};

据我所知, Socket 是一个套接字,并且是一个有效的。我怎么会收到那个错误呢?

4

1 回答 1

2

Socket在构造函数中将成员初始化为错误的值Winsock-NULL而不是INVALID_SOCKET. 它们不是相同的值。

WSAAsyncSelect()无论socket()成功还是失败,您都在调用。如果事情失败,您将显示错误消息,但当它们失败时您不会停止您的代码。您需要清理错误处理。

附带说明一下,您需要使用而不是手动DestroyWindow()发送消息。WM_DESTROY

试试这个:

class Winsock
{
public:
    Winsock();

    void Initialize(HWND);

    void ReceiveMsg();

private:
    SOCKET m_Socket;
    ...
};

Winsock::Winsock()
    : m_Socket(INVALID_SOCKET)
{
}

void Winsock::Initialize(HWND hwnd)
{
    SendMessage(hwnd, LOG_ADD, 0, (LPARAM)L"Initializing winsock... ");

    WSADATA wsaDat = {0};
    if (WSAStartup(MAKEWORD(2,2), &wsaDat) != 0)
    {
        SendMessage(hwnd, LOG_ADD, 0, (LPARAM)L"Winsock initialization failed");
        return;
    }

    SendMessage(hwnd, LOG_ADD, 0, (LPARAM)L"Done!\nCreating a socket... ");

    m_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (m_Socket == INVALID_SOCKET)
    {
        SendMessage(hwnd, LOG_ADD, 0, (LPARAM)L"Socket Creation failed");
        return;
    }

    SendMessage(hwnd, LOG_ADD, 0, (LPARAM)L"Done!\nRequesting Windows message-based notification of network events... ");

    if (WSAAsyncSelect(m_Socket, hwnd, WM_SOCKET, FD_CLOSE|FD_READ) != 0)
    {
        SendMessage(hwnd, LOG_ADD, 0, (LPARAM)L"WSAAsyncSelect failed");
        return;
    }

    /* More code */
}

.

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
    if(SUCCEEDED(CoInitialize(NULL)))
    {
        Game game;
        game.CreateResources(hInst);
        game.ShowMainScreen();
        ...
    }
}

void Game::CreateResources(HINSTANCE hInst)
{   
    m_hWnd = CreateWindowClass(hInst);
    pMessageLog = CreateMessageLog();
    pD2DResources = CreateD2DResources(m_hWnd);
    pWinsock = CreateWinsock();
    pWinsock->Initialize(m_hWnd);
}
于 2013-03-18T00:55:33.500 回答