CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    86

    How to get IPs of the machines in the lan???

    I am using VC++ in Visual Studio 2005.
    I have created the dialog based application. I want to broadcast the message in UDP to know which machines can receive the message & reply me back ( provided those machines are using same exe as I use to broadcast ).
    Can anybody tell how to do this???
    I could reach up to this...........

    Code:
          int BroadCastInitial;
    		int iBroadCast = 0;
    		int rflag = 0, flag =1, len = 0;
    
    		BroadCastInitial = UDPSockptr -> SetSockOpt( SO_BROADCAST, &flag, sizeof( int ) );
    
    		if ( !BroadCastInitial )
    		{
    			wsprintf(m_SocError, "SetSockOpt failed to set SO_BROADCAST:% d", UDPSockptr->GetLastError());
    			
                            delete UDPSockptr;
    			UDPSockptr = NULL;
    			AfxMessageBox ( m_SocError );
    			
    		}
    		
    		sockaddr_in m_RemoteAdd;
    		m_RemoteAdd.sin_family = AF_INET;
    		m_RemoteAdd.sin_addr.s_addr = inet_addr( "255.255.255.255" );
    		m_RemoteAdd.sin_port = htonl( INADDR_BROADCAST );
    		iBroadCast = UDPSockptr -> SendTo("AnyBody here me?",20, (const SOCKADDR*) &m_RemoteAdd, sizeof ( m_RemoteAdd ) );
    		
                    if ( !iBroadCast )
    		{
    			
    			wsprintf(m_SocError, " Broadcasting failed % d", UDPSockptr->GetLastError());
    			AfxMessageBox ( m_SocError );
    			
    		}
    		else
    		{
    			AfxMessageBox ( "Broadcasting successful" );
    				
    		}

  2. #2
    Join Date
    Dec 2008
    Posts
    86

    Re: How to get IPs of the machines in the lan???

    What I want simply, is to know the IP & port of the machines who can listen to broadcast message

  3. #3

    Re: How to get IPs of the machines in the lan???

    The ones that are listening to the port will receive the broadcast message. That's the definition of it being broadcast.

    To solve your problem, you have to have the ones listening to the port, reply back to the PC that sent the broadcast. That PC can get the IP address of the ones that replied from the reply packet. The "From" address is in the packet header.

  4. #4
    Join Date
    Dec 2008
    Posts
    86

    Re: How to get IPs of the machines in the lan???

    Quote Originally Posted by DHillard View Post
    The ones that are listening to the port will receive the broadcast message. That's the definition of it being broadcast.

    To solve your problem, you have to have the ones listening to the port, reply back to the PC that sent the broadcast. That PC can get the IP address of the ones that replied from the reply packet. The "From" address is in the packet header.
    Thank you for the reply
    I 'll tell in short what did I do....

    I created a dialog based application. then I added a class inherited from the CAsyncSocket
    In the dialog based class, I added a event handler to the button, When the button is clicked the socket is created then it broadcasts the message & listen to who is sending the reply.
    But I am getting the response as: Socket error 10035.
    I am at this point just listening to my PC still getting this error

    Code:
    void CModifiedSocketDlg :: SendMessageBroad()
    {
    		int BroadCastInitial;
    		int iBroadCast = 0;
    		int rflag = 0, flag =1, len = 0;
    
    		BroadCastInitial = UDPSockptr -> SetSockOpt( SO_BROADCAST, &flag, sizeof( int ) );
    
    		if ( !BroadCastInitial )
    		{
    			wsprintf(m_SocError, "SetSockOpt failed to set SO_BROADCAST:% d", UDPSockptr->GetLastError());
    			delete UDPSockptr;
    			UDPSockptr = NULL;
    			AfxMessageBox ( m_SocError );
    			
    		}
    		   len = sizeof(rflag);
    		
    			if ( UDPSockptr -> GetSockOpt( SO_BROADCAST, &rflag, &len, SOL_SOCKET ) && rflag == 0 )
    			{
    				wsprintf( m_SocError, "GetSockOpt failed to get SO_BROADCAST:%d ", UDPSockptr -> GetLastError());
    				delete UDPSockptr;
    				UDPSockptr = NULL;
    				AfxMessageBox ( m_SocError );			
    			}
    			else
    			{
    				UpdateData(FALSE);
    				AfxMessageBox( "Msg received" ); 
    				
    			}
    		
    		sockaddr_in m_RemoteAdd;
    		m_RemoteAdd.sin_family = AF_INET;
    		m_RemoteAdd.sin_addr.s_addr = inet_addr( "255.255.255.255" );
    		m_RemoteAdd.sin_port = htonl( SO_BROADCAST );
    		
    		iBroadCast = UDPSockptr -> SendTo("AnyBody here me?",20, (const SOCKADDR*) &m_RemoteAdd, sizeof ( m_RemoteAdd ) );
    		if ( !iBroadCast )
    		{
    			
    			wsprintf(m_SocError, " Broadcasting failed % d", UDPSockptr->GetLastError());
    			AfxMessageBox ( m_SocError );
    			
    		}
    		else
    		{
    			AfxMessageBox ( "Broadcasting successful" );
    			OnReceiveBroadcast();
    		}
    }
    This loop works fine as I can reach upto "Broadcasting successful" message..then ....

    Code:
    TCHAR    ReceivedBuf[ 70000 ];
    	CString  SendersIP;
    	TCHAR    m_ScError[25];
    	UINT     SendersPort;
    	int      ChekReceived;
    	
    	UpdateData(TRUE);
    
    	ChekReceived = UDPSockptr -> ReceiveFrom( ReceivedBuf, 69999, SendersIP, SendersPort );
    	
    	if ( ChekReceived == SOCKET_ERROR )      
     	{
    		wsprintf( m_ScError, "Failed to create Socket %i  Close And Restart application", GetLastError());
    		AfxMessageBox ( m_ScError );
    	}
    
    	else
    	{
    		//TCHAR sazError[256];
    		//wsprintf(sazError, "OnReceive bytes: %d", ChekReceived);
    		//AfxMessageBox (sazError);
    		AfxMessageBox( "I come in OnReceive" );
    		CString Disp = ReceivedBuf;
    		
    		m_ReceivedData += Disp;
    		UpdateData( FALSE );
    This loop gives me the error ...... Failed to create Socket 10035 Close And Restart application.

    Please suggest me why this is happening???

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