Thread: Program freezing..

  1. #1
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186

    Question Program freezing..

    Here is my code:

    Code:
    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    
    	HWND display = GetDlgItem(hwnd,IDL_DISPLAY);
    	int n = 0;
    
    	switch(Message)
    	{
    		case WM_INITDIALOG:
    		{
    			WSADATA w;
    
    			if(WSAStartup(0x0202,&w))
    				SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Error in initializing WinSock.");
    			else
    				SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Winsock initialized successfully.");
    			
    			n++;
    			
    			if(w.wVersion != 0x0202)
    			{
    				SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Wrong version of Winsock.");
    				WSACleanup();
    			}
    			else
    				SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Right version of Winsock (2.2)");
    
    			n++;
    
    		}
    		break;
    	
    		case WM_CREATE:
    
    		break;
    	
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case IDC_LISTEN:
    				{
    					SOCKET listener;
    					UINT listenport;
    
    					listenport = GetDlgItemInt(hwnd,IDC_SERVERPORT,NULL,false);
    					
    					listener = socket(AF_INET,SOCK_STREAM,0);
    
    					if(listener == INVALID_SOCKET)
    					{
    						SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Listening socket error.");
    						WSACleanup();
    					}
    					else
    						SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Listening socket created successfully");
    
    					n++;
    					
    					sockaddr_in serveraddr;
    
    					serveraddr.sin_family = AF_INET;
    					serveraddr.sin_port = htons(listenport);
    					serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    					if(bind(listener,(LPSOCKADDR)&serveraddr,sizeof(serveraddr)) == SOCKET_ERROR)
    					{
    						SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Error binding listening port.");
    						WSACleanup();
    					}
    
    					if(listen(listener,1) == SOCKET_ERROR)
    					{
    						SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Error listening.");
    						closesocket(listener);
    						WSACleanup();
    					}
    					else
    					{
    						SOCKET client;
    
    						client = accept(listener,NULL,NULL);
    
    						if(client == INVALID_SOCKET)
    						{
    							SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Cannont accept connection.");
    							closesocket(listener);
    							WSACleanup();
    						}
    						else
    						{
    							char *buffer;
    
    							while(buffer != "---")
    							{
    								buffer = "---";
    
    								recv(client,buffer,sizeof(buffer),0);
    
    								if(buffer != "---")
    								{
    									if(strlen(buffer) > 0)
    									{
    										SendMessage(display,LB_ADDSTRING,n,(LPARAM)buffer);
    										n++;
    									}
    									else
    									{
    										closesocket(listener);
    										SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Connection closed");
    										WSACleanup();
    									}
    								}
    							}
    						}
    					}										
    				}
    				break;
    			}
    		break;
    
    		case WM_CLOSE:
    			EndDialog(hwnd,0);
    		break;
    		
    		case WM_DESTROY:
    		
    		break;
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    When I run the program and click listen it freezes. I think I have the problem singled out to the "accept". If I take out all the code from accept and below, it works fine. If I just leave the accept and take out the socket code below it, it freezes. What could be the problem?
    the best things in life are simple.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I'm suggesting you use asynchronous sockets. I think it freezes is because you are using blocking sockets. Have you tried connecting to it after it 'freezes'? If you would like to convert to asynchronous sockets (they handle like windows messages, less bugs) contact me and I can help you.
    Last edited by neandrake; 11-17-2003 at 06:02 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Do you even have a client connecting? If not, then of course it will "freeze", since it will block until the socket is readable (i.e. there is a client waiting to connect). You could, as neandrake suggested, convert to asynchronous sockets, or you could use a loop of some sort using select() to determine whether or not the listening socket is readable (and handle your windows messages and all that somewhere in the loop), and then once select() returns that there is a client(s) connecting, then you can call accept().
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program freezing
    By Beowolf in forum C Programming
    Replies: 2
    Last Post: 09-10-2007, 05:38 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM