Advertisement

Async Sockets Question

Started by December 10, 2003 12:01 AM
6 comments, last by ChaosPhoenix 21 years, 2 months ago
I''ve got a console application that uses Asynchronous sockets so that the game action isn''t frozen while it waits for a connection the only problem is it doesn''t seem to be recieving any of the Windows Msgs like FD_ACCEPT, FD_READ, FD_CONNECT, or FD_WRITE. Everything seems to be set up but I noticed that I''ve been using the winsock.h library instead of the winsock2.h. Is winsock2.h required for those windows messages to be recieved or for Async sockets to work at all? I tried changing it over but got TONS of compile errors from the winsock2.h file, so I assume I must be missing either a libary or another header file that winsock2.h requires as right now I''m just including winsock.h and WS2_32.lib. Cliff Notes: 1. is winsock2.h required for Async sockets? 2. What header files and libraries are required for winsock2?
1. Winsock 2 is not required for WSAAsyncSelect() to work. Winsock 1.1 should work fine.

2. To use Winsock 2 you need the winsock2.h header and the WS2_32.lib library (at least on MSVC, might be different if using a different compiler).

--

If you''re using a console application, how are you generating the window handle needed for WSAAsyncSelect()?
Advertisement
Odd.....

Well here is my message processing function, if anyone can see if they spy anything wrong with this I would appreciate it. If not I''ll look else where in my program for the problem.

void CNetwork::NetMessageHandler(){	LPARAM lParam = NULL;	int nret;	switch(WSAGETSELECTEVENT(lParam))	{	case FD_CONNECT:		con << "FD_CONNECT called!\n";				break;	case FD_READ:		ServerRecvData();		con << "Message recieved!\n";		Display();		con << "Sending back...\n";		break;	case FD_ACCEPT:		Client = accept(listening, NULL, NULL);		WSAAsyncSelect(Client, hWnd, WM_WSAASYNC, FD_WRITE | FD_READ | FD_CLOSE);		if (Client == INVALID_SOCKET)		{			nret = WSAGetLastError();			con << Red << "Invalid Socket(Client)! Error Code : " << nret << "!\n" << Def;			WSACleanup();					}		con << Green << "Connected!\n" << Def;		break;	case FD_WRITE:				break;	case FD_CLOSE:		ClientClose();		ServerClose();		break;	}} 


The first thing you do is set lParam to 0 and then you switch on lParam. This doesn''t seem kosher.
WSAGETSELECTEVENT should fill in lParam with the appropriate flag for me if I read it''s decleration correctly.
You didn''t. WSAGETSELECTEVENT() is just a macro the separates out the LOWORD of lParam. The LOWORD of 0 is 0.
Advertisement
ah, so what is used to populate the LPARAM?
The windows message. When you call WSAAsyncSelect() you specify a window handle and a message identifier. When the an event is signaled a windows message gets sent to the window, and the message contains the WPARAM and LPARAM designating the socket, the event signalled and any error flags.

This topic is closed to new replies.

Advertisement