multiple asynchronous connections

Started by
5 comments, last by hplus0603 16 years ago
Hello all creating a network lobby for a game. So far what i have is a server and a client program, the clients can connect to the the server and talk to each other via the server. I am doing this through TCP and using asynchronous socks ( WSAAsyncSelect () ) (using c++ btw) Im now trying to get the client program to host a room which involves them binding to a port and accpeting connections whilst still connect to the server. Other clients will be able to connect to them and still also connected to the server. Both clients in the hosted room and host will DC from the server when the game begins. (when the client is hosting or a client is connecting to the hosting client the socks are set to asynchronous mode after they have connected or after they have bound and listened) What my problem is that when a client hosts and binds to a port and when another client connects (hosting client can have up to 8 connections) to the hosting client. The asynchronous sock does not seem to pick them up, i have put a printf's in the FD_ACCEPT bit for the hosting client and a printf for in the FD_CONNECT bit for the connecting client none of these printf show up after a client "connects" to the hosting client. What i have done is the following i added two new extra flags for hosting client and the client joning the host.

// flag for connection to server
#define WM_WSASERVCONNECASYNC		(WM_USER +2) 

// flag for hosting client to recive connection form peers
#define WM_WSAHOSTINGCONNECASYNC	(WM_USER +300)

// flag for connection to the hosting peer
#define WM_WSACONNECTOHOSTASYNC		(WM_USER +400)

In the main.cpp I create the class instances for the connection to the server socket, connetion to the host client socket, a socket for when they are hosting, and a array instance socket for the peers when the client is hosting.

CWinSockObject ServerConnectionSoc; // socket to connect to the server
CWinSockObject ConnectionToHostSoc; // socket to conect to the hosting peer

CWinSockObject HostingConnectionSoc; // sock for the hosting peer.

// sock for the connecting peers

CWinSockObject HostPeerConnectionSoc[MAX_HOSTPEERS]; 

then in the dialog prodcedure function i done this.....

BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)									
     {
       case WM_WSACONNECTOHOSTASYNC:
			if (WSAGETSELECTERROR(lParam))
			{
				// pfrintf error
			}

			switch (WSAGETSELECTEVENT(lParam))
			{
				case FD_READ:
					break;

				case FD_CONNECT:
					// printf connected to host
					break;

				case FD_CLOSE:
					// printf connection lost from host
					break;
			}

		break;

	case WM_WSAHOSTINGCONNECASYNC:

			if (WSAGETSELECTERROR(lParam))
			{
				// pfrintf error
			}

			switch (WSAGETSELECTEVENT(lParam))
			{
				case FD_READ:
					break;
				case FD_ACCEPT:
					// printf some 1 has connected
					break;
				case FD_CLOSE:
					// printf connection lost
					break;

			}

		break;


		case WM_WSASERVCONNECASYNC:
			if (WSAGETSELECTERROR(lParam))
			{
				
			}
			switch (WSAGETSELECTEVENT(lParam))
			{
				case FD_READ:
					// printf recived data from server
					break;

				case FD_CONNECT:
					// connected to server
					break;

				case FD_CLOSE:
					// lost connection to server
					break;

			}break;  
     }
}

In win main..... When the user is connecting to the server they will connect using the servers ip address and tcp port number and set the socket to async mode. this works fine If the client is hosting they will bind to a port, call the listen function then set that socket to async mode. In FD_ACCEPT under WM_WSAHOSTINGCONNECASYNC they will accept any connection requests. If the client is connecting to the hosting peer they will connect to the hosting peers ip address and tcp port then set the port to async mode. What i am i doing wrong ? hope i have explained the problem well
Advertisement
I'm not a C++ socket user but it looks to me like you're only handling your custom messages. I would expect you would need to handle some standard WSA messages for connection, disconnection and receiving data.
mmmm im pretty sure it does not matter what number for the WM_USER bit ive seen people have like 300 (WM_USER+300) etc......
Async sockets are, in general, not a good idea. The reason is that they perform poorly, and they have all kinds of gotchas in how you handle the messages. Typically, you'll want to set a flag, and then handle the actual event in the top loop, not in the message handler. However, if that's what you're doing, you might as well just use select() and a timer, which is a simpler way to do it, and has less bugs.
enum Bool { True, False, FileNotFound };
ill try select, tho i heard Async sockets where reliable on a couple on tutorials, what is the best way to handle multiple connections? Like the they was im trying to do, using select or theading?

Thanks
btw wht did you mean by

Quote:
Typically, you'll want to set a flag, and then handle the actual event in the top loop, not in the message handler.



do you mean put it in win main?
When using asynchronous sockets, you want to just set a flag when you receive the messages (or queue yourself a command functor, or whatever).
Then, inside WinMain() (or wherever your loop is), after your DispatchMessage() call, you test for the flags, and do the right thing if they're set. Or you dispatch the queue of functors, or however you choose to do this.
One of the biggest gotchas with WSA async sockets is that doing I/O on the sockets within the messages may case bad behavior.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement