Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

Is my Winsock Asynchronous socket setup properly for listening?

This topic is 5081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Im using winsock asynchronous sockets. So far I've set it up listening. But I am not sure if this is right. Could you tell me if I got my code in the right places? Thanks WM_USER+1 is what I have assigned the asynchronous socket to. //start listening button
SOCKET s, client[3];
int number_of_clients;
void CHostDlg::OnButtonHostgameStartListening() 
{
	number_of_clients=0;
	s = socket (AF_INET, SOCK_STREAM, 0);

	sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons (5001);
	addr.sin_addr.s_addr = htonl (INADDR_ANY);
	if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
	{
		MessageBox("Winsock failed to bind port", "Error");
		WSACleanup ();
	}

	if (listen(s,3)==SOCKET_ERROR)
	{ 
		MessageBox("Winsock failed to start listening", "Error");
		WSACleanup ();
		editConnectionStatus->SetWindowText("Disconnected");
	}

	WSAAsyncSelect(s, GetSafeHwnd(), WM_USER+1, FD_WRITE | FD_CONNECT | FD_READ | FD_CLOSE);

}




//disconnect
void CHostDlg::OnButtonHostgameCloseConnections() 
{
	shutdown (s, SD_SEND);  
	closesocket (s);  
}





//message handler, handles connection accepts
LRESULT CHostDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	switch( message ) 
	{ 
		case WM_USER+1:
			if (WSAGETSELECTERROR(lParam))
			{
				WSACleanup ();
				return 0;
			}

			switch (WSAGETSELECTEVENT(lParam))
			{
				case FD_ACCEPT:
				   while (number_of_clients < MAX_CLIENTS)
					{
						client[number_of_clients] = accept (s, client_sock[number_of_clients], &addr_size); 
						if (client[number_of_clients] == INVALID_SOCKET)
						{
							WSACleanup ();
							return 0;
						}
						else
						{ 
							number_of_clients++;
						}
					} 
					
					break;
				case FD_READ:
					break;
				case FD_CONNECT:
					break;
				case FD_CLOSE:
					break;
				default:
					break;
			}
		break;
	}
	return CDialog::WindowProc(message, wParam, lParam);
}






[Edited by - johnnyBravo on August 19, 2004 7:24:09 AM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!