Here is Main.cpp up to the point where the error happens:
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 return the private HWND m_hWnd.
Here is Game::CreateRessources(HINSTANCE):
void Game::CreateRessources(HINSTANCE hInst)
{
m_hWnd=CreateWindowClass(hInst);
pMessageLog=CreateMessageLog();
pD2DResources=CreateD2DResources(m_hWnd);
pWinsock=CreateWinsock();
}
There is CreateWinsock():
Winsock* CreateWinsock()
{
Winsock* pWinsock=new Winsock;
return pWinsock;
}
Winsock::Winsock:
Winsock::Winsock() : Socket(NULL) { }
And finally, 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 */
}
The line if(WSAGetLastError()==WSAENOTSOCK) returns true. WSAENOTSOCK means the following:
"Socket operation on nonsocket. An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid."
Edit: There is my Winsock class:
class Winsock{
public:
Winsock();
void Initialize(HWND);
void ReceiveMsg();
private:
SOCKET Socket;
static const char* server;
static const int port;
};
As far as I can tell, Socket IS a socket, and a valid one. How comes I receive that error anyway?
SendMessageone of your functions? Can't find it in the winsock API, there's onlysend. - us2012WndProcby the time you reachWSAAsyncSelect? - Roman R.