Sign in to follow this  
badmoon

Winsock: Trying to get client to recv asynchronously from server

Recommended Posts

Hi I currently have a server program that creates a socket, binds it to the local machine and waits for one client via the accept function. I then run my client program which creates a socket,connects to the server, and then uses the WSAAsyncSelect function to make the socket non-blocking. In the server program once it detects the connection and the call to accept returns, it calls the send function to send some data. The client in the meantime is calling recv every frame which returns immediately as I set the socket to non-blocking. However when I finally get the message in my WindowProc function, the pointer which I specified in my recv call doesn't get filled with the data sent! Does anyone know why? Here's the code:
-------------------------SERVER-----------------------------

	ghWnd = MakeWindow(hInstance,50,50,100,100);

	//Create the listening socket
	gxListenSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

	//Bind it
	SOCKADDR_IN xSockAddr;
	xSockAddr.sin_addr.s_addr = INADDR_ANY;
	xSockAddr.sin_port = htons(80);
	xSockAddr.sin_family = AF_INET;
	if( bind( gxListenSocket, (LPSOCKADDR)&xSockAddr, sizeof(xSockAddr) ) == SOCKET_ERROR )
	{
		MessageBox( ghWnd, "Failed to bind listening socket", "Error", MB_OK );DeInit();return 0;
	}

	//Listen
	listen( gxListenSocket, 1 );

	//Accept client
	gxClientSocket = accept( gxListenSocket, NULL, NULL );

	send( mxClientSocket,(char*)&gnDataForClient,4,0 );


-------------------CLIENT-------------------------------
	ghWnd = MakeWindow(hInstance,40,40,500,500);

	WSADATA xWSAData;

	WSAStartup( MAKEWORD(1,1), &xWSAData );

	//Create socket
	gxServerSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

	//Connect
	SOCKADDR_IN xAddr;

	xAddr.sin_family = AF_INET;
	xAddr.sin_addr.s_addr = inet_addr("192.168.110.99");
	xAddr.sin_port = htons(80);
	if( connect( gxServerSocket, (SOCKADDR*)&xAddr, sizeof(xAddr) ) == SOCKET_ERROR )
	{
		MessageBox(ghWnd,"Failed connection","Client",MB_OK);
		PostQuitMessage(0);
	}

	WSAAsyncSelect( mxServerSocket, ghWnd, 9999, FD_READ );

	//Message loop
	MSG xMsg;
	while( TRUE )
	{
		if( PeekMessage( &xMsg, NULL, 0, 0, PM_REMOVE ) )
		{
			if( xMsg.message == WM_QUIT )
			{
				break;
			}
			TranslateMessage( &xMsg );
			DispatchMessage( &xMsg );
		}
		else
		{
			//User code
			recv( gxServerSocket, (char*)&gnDataFromServer, 4, 0 );
		}
	}



LRESULT WINAPI MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	int i;
	switch( uMsg )
	{
	case WM_LBUTTONDOWN:
		mnDrawTri = 1;
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	case 9999:
		//GET HERE BUT gnDataFromServer HASN'T BEEN UPDATED WITH SENT DATA
		return 0;
	}
	return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

Maybe the problem is that WSAAsyncSelect only works on a server socket. But that would make it a really rubbish function. Somebody please help.

Share this post


Link to post
Share on other sites
When you get that particular notify message in your window proc the corresponding recv has not been called yet.

You should place the recv inside the window proc, only on receiving your notify message.

WSAAsyncSelect works for both kinds of socket.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this