Thread: Simple Client - Server problems (Async Sockets )

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Simple Client - Server problems (Async Sockets )

    im trying to make a Simple Client - Server application using Async Sockets, that will support Multiple connection on server side

    i have read several tutorials and examples on the net but i have some problems and i hope some1 can help me fix them

    1) When i close Server or Client , i cannot reconnect back , even with FD_CLOSE event code

    2) FD_ACCEPT: doesn't display a message box when its connected , why is there something wrong?

    3) Does this Server code is able to accept multiple connection at once? or ill need to recall the listen function each time i make a connection?

    the code doesn't have anything more that sockets initialization and connection
    hope some1 can point out the code errors.


    Server Code

    Code:
    #include <winsock2.h>
    #include <windows.h>
    
    #pragma comment(lib,"wsock32.lib")
    #define WM_WSAASYNC (WM_USER +1)
    #define WM_SOCKET			104
    
    LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
    void SockInit(HWND hwnd);
    void SockListen(HWND hWnd);
    
    
    SOCKET			hSock;
    SOCKET			sTemp;
    SOCKADDR_IN		ServAdr;
    sockaddr sockAddrClient;
    
    int	SPort = 4455;
    
    void SockListen(HWND hWnd)
    
    {	hSock = socket(AF_INET,SOCK_STREAM,0);
    	WSAAsyncSelect(hSock,hWnd,WM_WSAASYNC,FD_ACCEPT|FD_CLOSE|FD_READ);
    	ServAdr.sin_family=AF_INET;
    	ServAdr.sin_addr.s_addr=htonl(INADDR_ANY);
    	ServAdr.sin_port=htons(SPort);
    	bind(hSock,(LPSOCKADDR)&ServAdr,sizeof(struct sockaddr));
    	listen(hSock, 1);
    }
    
    
    void SockInit(HWND hwnd)
    {
    	WORD	wVersionRequested;
    	WSADATA wsaData;
    	int		err;
    	wVersionRequested = MAKEWORD( 2, 0 );
        err = WSAStartup( wVersionRequested, &wsaData );
        if ( err != 0 )
    		PostQuitMessage(1);
    
        if ( LOBYTE( wsaData.wVersion ) != 2 ||
    		 HIBYTE( wsaData.wVersion ) != 0 )
    	{
    	    WSACleanup();
    		PostQuitMessage(0);
    	}
    }
    
    LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
    
    	switch(msg)
        {
    		case WM_CREATE: 
    			{
    				SockInit(hWnd);
    				SockListen(hWnd);
    			}
    			break;
    
    		case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    			shutdown(wParam,SD_BOTH);
    			closesocket(wParam);
    			WSACleanup();
    			return 0;
    		}
    		break;
    
    		case WM_SOCKET:
    		{
    
    
    			switch(WSAGETSELECTEVENT(lParam))
    				{
    
    
    				case FD_ACCEPT:
    				{
    					int size=sizeof(sockaddr);
    					sTemp =  accept(wParam,&sockAddrClient,&size);
    					MessageBox(hWnd,"Server Accepted","Connection!",MB_ICONINFORMATION|MB_OK);
    				}
    				break;
    
    				case FD_READ:
    				{
                                      //Not done yet
    				}
    				break;
    
    				case FD_CLOSE:
    				{
    					WSACleanup();
    					closesocket(wParam);
    				}
    				break;
    
    			}
    
    		}
    	}
        
        return(DefWindowProc(hWnd, msg, wParam, lParam));
    }
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
    {
    MSG msgs;
    HWND hwnd;
    WNDCLASSEX wClass;
    
    	ZeroMemory(&wClass,sizeof(WNDCLASSEX));
    	wClass.cbClsExtra=NULL;
    	wClass.cbSize=sizeof(WNDCLASSEX);
    	wClass.cbWndExtra=NULL;
    	wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
    	wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    	wClass.hIcon=NULL;
    	wClass.hIconSm=NULL;
    	wClass.hInstance=hInst;
    	wClass.lpfnWndProc=(WNDPROC)WinProc;
    	wClass.lpszClassName="Window Class";
    	wClass.lpszMenuName=NULL;
    	wClass.style=CS_HREDRAW|CS_VREDRAW;
    
    	RegisterClassEx(&wClass);
    
    
    	HWND hWnd=CreateWindowEx(NULL,"Window Class","Client",WS_OVERLAPPEDWINDOW,    //WS_POPUP | WS_VISIBLE
    							400,
    							200,
    							500,
    							300,NULL,NULL,hInst,NULL);
    
        ShowWindow(hWnd,nShowCmd);
    
    	ZeroMemory(&msgs,sizeof(UINT));
    
    		while(GetMessage(&msgs,NULL,0,0))
    	{
    
    		TranslateMessage(&msgs);
    		DispatchMessage(&msgs);
    	}
    	return 0;
    }
    Client Code
    Code:
    #include <winsock2.h>
    #include <windows.h>
    
    #pragma comment(lib,"wsock32.lib")
    #define WM_WSAASYNC (WM_USER +1)
    #define WM_SOCKET 104
    
    LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
    void SockInit(HWND hwnd);
    void SockListen(HWND hWnd);
    void CloseSocket();
    
    SOCKET			hSock;
    
    
    SOCKADDR_IN		ServAdr;
    int	SPort = 4455;
    
    void Connect(HWND hWnd)
    
    {	
    	int ConRes;
    
    	Sleep(1000);
    		
    
            begin:
    		Sleep(1000);
    
    		ServAdr.sin_family = AF_INET;
    		ServAdr.sin_addr.s_addr=inet_addr("127.0.0.1");
    		ServAdr.sin_port = htons (SPort);
    		hSock = socket (AF_INET,SOCK_STREAM,0);
    
    	    ConRes=connect (hSock, (struct sockaddr *)&ServAdr,sizeof(ServAdr));
    			if (ConRes==-1)
    			{
    
    			 goto begin;
    			}
    			else
    			{
    			
    			WSAAsyncSelect(hSock,hWnd,WM_WSAASYNC,FD_READ|FD_CLOSE|FD_CONNECT);
    			}
    	return;
    
    }
    
    
    void SockInit(HWND hwnd)
    {
    	WORD	wVersionRequested;
    	WSADATA wsaData;
    	int		err;
    	wVersionRequested = MAKEWORD( 2, 0 );
        err = WSAStartup( wVersionRequested, &wsaData );
        if ( err != 0 )
    		PostQuitMessage(1);
    
        if ( LOBYTE( wsaData.wVersion ) != 2 ||
    		 HIBYTE( wsaData.wVersion ) != 0 )
    	{
    	    WSACleanup();
    		PostQuitMessage(0);
    	}
    }
    
    LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
    
    	switch(msg)
        {
    		case WM_CREATE: 
    			{
    				SockInit(hWnd);
    				Connect(hWnd);
    				
    			}
    			break;
    
    		case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    			shutdown(wParam,SD_BOTH);
    			closesocket(wParam);
    			WSACleanup();
    			return 0;
    		}
    		break;
    
    
    	case WM_SOCKET:
    		{
    
    			switch(WSAGETSELECTEVENT(lParam))
    				{
    
    				case FD_CONNECT:
    				  {
    					MessageBox(hWnd,"Server connected","Connection!",MB_ICONINFORMATION|MB_OK);
    				  } 
    				  break;
    
    
    				case FD_READ:
    				{
    					//Not done yet
    
    				}
    				break;
    
    				case FD_CLOSE:
    				{
    					WSACleanup();
    					closesocket(wParam);
    					Connect(hWnd);
    				}
    				break;
    
    			}
    
    		}
    	}
        return(DefWindowProc(hWnd, msg, wParam, lParam));
    }
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
    {
    MSG msgs;
    HWND hwnd;
    WNDCLASSEX wClass;
    
    	ZeroMemory(&wClass,sizeof(WNDCLASSEX));
    	wClass.cbClsExtra=NULL;
    	wClass.cbSize=sizeof(WNDCLASSEX);
    	wClass.cbWndExtra=NULL;
    	wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
    	wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    	wClass.hIcon=NULL;
    	wClass.hIconSm=NULL;
    	wClass.hInstance=hInst;
    	wClass.lpfnWndProc=(WNDPROC)WinProc;
    	wClass.lpszClassName="Window Class";
    	wClass.lpszMenuName=NULL;
    	wClass.style=CS_HREDRAW|CS_VREDRAW;
    
    	RegisterClassEx(&wClass);
    
    
    	HWND hWnd=CreateWindowEx(NULL,"Window Class","Server",WS_OVERLAPPEDWINDOW,    //WS_POPUP | WS_VISIBLE
    							400,
    							200,
    							300,
    							100,NULL,NULL,hInst,NULL);
    
        ShowWindow(hWnd,nShowCmd);
    
    	ZeroMemory(&msgs,sizeof(UINT));
    
    		while(GetMessage(&msgs,NULL,0,0))
    	{
    
    		TranslateMessage(&msgs);
    		DispatchMessage(&msgs);
    	}
    	return 0;
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    1) Call WSACleanup() after closesocket(), not before.

    2) Ditch your use of goto - a while loop is a much better construct there.

    3) Of course you won't be able to reconnect if you close the server... what were you expecting?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    Quote Originally Posted by Cactus_Hugger View Post
    1) Call WSACleanup() after closesocket(), not before.

    2) Ditch your use of goto - a while loop is a much better construct there.

    3) Of course you won't be able to reconnect if you close the server... what were you expecting?
    thanks for the help, what i ment with the reconnect thing is that if i close and reopen the client (server is still open) and vice-versa , i cannot establish a connection, i will have to close both server and client to connect again

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    What is WM_SOCKET? You're telling WSAAsyncSelect to send you WM_USER + 1 or WM_WSAASYNC, but your WndProc seems to be looking for a WM_SOCKET. Switch WM_SOCKET to the WM_WSAASYNC.

    Also, why do you pass listen() a 1?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    thanks it works now, i also replaced 1 with SOMAXCONN

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Commerical MMORPG Developer Opp
    By Th3Guy in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 01-22-2007, 11:28 AM
  2. Server Client Messaging Program
    By X PaYnE X in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-04-2004, 05:20 PM
  3. [SOCKETS] Solaris server in C, NT client in VB
    By Daveg27 in forum C Programming
    Replies: 3
    Last Post: 05-23-2002, 10:02 AM
  4. Simple Server problems...
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 08:51 PM
  5. Socket Problems
    By (TNT) in forum Windows Programming
    Replies: 4
    Last Post: 08-18-2001, 06:59 AM