Hello everybody!
I'm working on a small personal project which has to use async sockets. So far I've not yet finished the GUI (since I'm coding it with pure WinAPI, it's going to take some time) but i thought to give it a shot and see if it's processing all specified messages in WndProc but so far i realized that it isn't processing network messages. Here's a sample code from my project:
int WINAPI WinMain(HINSTANCE currentInst,
HINSTANCE prevInst,
LPSTR args,
int showVar)
{
WSADATA wsaData;
if(WSAStartup(0x0202, &wsaData))
{
MessageBox(NULL,"Call to WSAStartup() failed at line #", "Error!", MB_ICONEXCLAMATION | MB_OK);
WSACleanup();
return wsaFail;
}
else
{
if(wsaData.wVersion != 0x0202)
{
MessageBox(NULL,"Wrong socket version! at line #", "Error!", MB_ICONEXCLAMATION | MB_OK);
WSACleanup();
return wsaFail;
}
}
WNDCLASSEX windowClass;
HWND handleToWindow;
SOCKET serverSocket;
sockaddr_in serverSockAddrIn;
MSG Msg;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_NOCLOSE | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.lpszClassName = class_name;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = currentInst;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 6);
windowClass.lpszMenuName = NULL;
windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&windowClass))
{
MessageBox(NULL, "Window Registration Failed at EntryPoint.cpp line #", "Error!", MB_ICONEXCLAMATION | MB_OK);
return windowRegistrationFail;
}
handleToWindow = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_OVERLAPPEDWINDOW | WS_EX_TOPMOST,
class_name,
"Server Console",
WS_OVERLAPPED| WS_VISIBLE,
400, 500, 800, 500, NULL, NULL, currentInst, NULL);
if(handleToWindow == NULL)
{
MessageBox(NULL, "Window Creation Failed at line #", "Error!", MB_ICONEXCLAMATION | MB_OK);
return windowCreationFail;
}
UpdateWindow(handleToWindow);
memset(&serverSockAddrIn, 0, sizeof(serverSockAddrIn));
serverSockAddrIn.sin_family = AF_INET;
serverSockAddrIn.sin_port = htons(2567);
serverSockAddrIn.sin_addr.s_addr = htonl(INADDR_ANY);
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(serverSocket == INVALID_SOCKET)
{
MessageBox(NULL, "Socket Creation Failed at EntryPoint.cpp line#", "Error!", MB_ICONEXCLAMATION | MB_OK);
WSACleanup();
return socketFail;
}
WSAAsyncSelect(serverSocket, handleToWindow, NetworkNotification, FD_ACCEPT | FD_READ | FD_WRITE | FD_CLOSE);
if(bind(serverSocket, (sockaddr*)&serverSockAddrIn, sizeof(serverSockAddrIn)) == SOCKET_ERROR)
return 0;
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
So far this is the whole entry point of my program. Keep in mind that it's not finished yet. Please do not reply with coments like "You should error check at x" etc. if it's not a possible fix for my problem.
WndProc (showing only network messages):
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case NetworkNotification:
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
{
MessageBox(hwnd, "Success!", "Test", NULL);
break;
}
case FD_READ:
break;
case FD_WRITE:
break;
case FD_CLOSE:
break;
}
}
case WM_COMMAND:
{
}
case WM_CREATE:
{
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}
I repeat...it's not the whole WndProc here. Unless it's really needed i wouldn't like to post it all.
As far as I'm aware, it should show the message box when a client is trying to connect, but of course.. since i'm not calling accept() the client won't be able to connect but it still should trigger a FD_ACCEPT event...right ?
Ahh...i almost forgot..
#define NetworkEvent (WM_USER + 5)
modified on Friday, January 8, 2010 8:32 AM
|