这是 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 是一个套接字,并且是一个有效的。我怎么会收到那个错误呢?