CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 67

Threaded View

  1. #25
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    i have changed so much code so i started again.
    here is the current code:
    Code:
    WSADATA wsaData; 
    		SOCKET ListeningSocket, NewConnection; 
    		SOCKADDR_IN ServerAddr; 
    		int Port = 7171;
    		char recvbuff[1024];
    		int i, nlen;
    
    		if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
    		{
    			CString text;
    			text.Format(_T("ERROR WSAStartup failed: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    			exit (1);
    		}
    	
    		//ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    		  ListeningSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
    		if (ListeningSocket == INVALID_SOCKET)
    		{
    			CString text;
    			text.Format(_T("ERROR at socket, error code: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    			WSACleanup();
    			exit (1);
    		}
    	
    
    		ServerAddr.sin_family = AF_INET;
    		ServerAddr.sin_port = htons(Port);
    		ServerAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    
    		if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
    		{
    			CString text;
    			text.Format(_T("ERROR bind failed, Error code: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    			closesocket(ListeningSocket);
    			WSACleanup();
    			exit (1);
    		}
    
    		if (listen(ListeningSocket, 5) == SOCKET_ERROR)
    		{
    			CString text;
    			text.Format(_T("ERROR listen: Error listening on socket: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    			closesocket(ListeningSocket);
    			WSACleanup();
    			exit (1);
    		}
    
    		while(1)
    		{
    			NewConnection = SOCKET_ERROR;
    			while(NewConnection == SOCKET_ERROR)
    			{
    				NewConnection = accept(ListeningSocket, NULL, NULL);
    
    				//getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
    
    				memset(&ServerAddr, 0, sizeof(ServerAddr));
    				nlen = sizeof(ServerAddr);
    
    				int ret = sendto(ListeningSocket, recvbuff, strlen(recvbuff), 0, (sockaddr*)&ServerAddr, nlen);
    			    if(ret < 0)
    				{
    					CString text;
    					text.Format(_T("ERROR Error broadcasting to the clients: %d"), WSAGetLastError());
    					AfxMessageBox(text);
    				}
    
    				//getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
    
    			}
    				
    		}
    
    		if( shutdown(NewConnection, SD_SEND) != 0)
    			printf("\n\nServer: Well, there is something wrong with the shutdown. The error code: %ld\n", WSAGetLastError());
    			CString text;
    			text.Format(_T("ERROR Something wrong with shutdown, error code: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    		
    
        SetTimer(0x01, 100, NULL);
    }
    now when i run it the error message "ERROR bind failed, Error code: 10049" displays
    that error means
    Cannot assign requested address.
    The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer. This can also result from connect, sendto, WSAConnect, WSAJoinLeaf, or WSASendTo when the remote address or port is not valid for a remote computer (for example, address or port 0).
    Last edited by beginner91; March 20th, 2013 at 05:09 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured