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

Thread: Broadcasting in UDP

  1. #1
    Join Date
    Dec 2008
    Posts
    86

    Broadcasting in UDP

    I want to know the IP & port of the machines. For this purpose I am broadcasting any message through UDP socket.

    But I dont receive any message(even from my machine).
    Is it possible to code such a way that when dialog box is created, the constructor itself broadcast the message & listen to the reply. If reply occurs it will find out the IP & port of that machine.

    If it is possible in VC++. PLease let me know how. I have done this(sorry, for it is wrong) but not up to the mark..............

    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" );
    		}
    		
            len = sizeof(rflag);
    
    		if ( UDPSockptr -> GetSockOpt( SO_BROADCAST, &rflag, &len ) && rflag == 0 )
    		{
    			wsprintf( m_SocError, "GetSockOpt failed to get SO_BROADCAST:% d", UDPSockptr -> GetLastError());
    			delete UDPSockptr;
    			UDPSockptr = NULL;
    			AfxMessageBox ( m_SocError );			
    		}
    		else
    		{
    			AfxMessageBox( "Msg received" );  
    		}
    I called this function in the constructor. It gets executed without error. But, sent message is not displayed

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,562

    Re: Broadcasting in UDP

    Note that the most of network administrators do NOT allow any UDP (neither sockets nor mailslots).
    However, your own computer is supposed to receive those messages.
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2007
    Location
    Birmingham, UK
    Posts
    360

    Re: Broadcasting in UDP

    @VictorN - are you sure about your statement that "most of network administrators do NOT allow any UDP" as I've never come across that situation. Or did you mean broadcast messages?

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,562

    Re: Broadcasting in UDP

    Quote Originally Posted by Edders View Post
    ... Or did you mean broadcast messages?
    You are right! I meant broadcast messages. Sorry!
    Victor Nijegorodov

  5. #5
    Join Date
    Dec 2008
    Posts
    86

    Re: Broadcasting in UDP

    In my network broadcast messages are allowed as I have checked out the same using another EXE.

    The issue is how to achieve this, I mean get the IP of the machine using the application

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,562

    Re: Broadcasting in UDP

    Have a look at this thread and at the pointed out FAQ.
    Victor Nijegorodov

  7. #7
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: Broadcasting in UDP

    Scorrpeio, I should expect a while() { ... } block that contains the messages received code, not only a single GetSockOpt() call.

  8. #8
    Join Date
    Dec 2008
    Posts
    86

    Re: Broadcasting in UDP

    @Maximus X, I dont get your point clearly

    @Victor, Thank you for the link. But, corresponding discussion was about getting the IP/name of the machine which uses the application(sorry, I did mistake in last reply by saying that I want such function which could give me the name of only single PC )
    I want to know the IP/names of all the machines which are using the application. just like in LAN messenger we can see the available names.

    Please suggest me the solution

  9. #9
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: Broadcasting in UDP

    Quote Originally Posted by scorrpeio View Post
    @Maximus X, I dont get your point clearly
    At this moment your code fails because I cannot received instantaneous good result. So you need to wait the right answer in a while block.
    I suppose and guess that you are using a second thread code for this broadcast code. Well from the application interface, you can control this while using a bool flag:
    Code:
    while(!pObject->m_bStatus)
    {
      		if ( UDPSockptr -> GetSockOpt( SO_BROADCAST, &rflag, &len ) && rflag == 0 )
    		{
    			wsprintf( m_SocError, "GetSockOpt failed to get SO_BROADCAST:% d", UDPSockptr -> GetLastError());
    			delete UDPSockptr;
    			UDPSockptr = NULL;
    			AfxMessageBox ( m_SocError );			
    		}
    		else
    		{
    			AfxMessageBox( "Msg received" );  
    		}     
    }
    But be careful... don't use AfxMessageBox() inside of while block because it stops your thread over it.

  10. #10
    Join Date
    Dec 2008
    Posts
    86

    Re: Broadcasting in UDP

    Quote Originally Posted by VictorN View Post
    Have a look at this thread and at the pointed out FAQ.
    Thank you for the link. But, corresponding discussion was about getting the IP/name of the machine which uses the application(sorry, I did mistake in last reply by saying that I want such function which could give me the name of only single PC )
    I want to know the IP/names of all the machines which are using the application. just like in LAN messenger we can see the available names.

    Please suggest me the solution

  11. #11
    Join Date
    Jan 2009
    Posts
    8

    Re: Broadcasting in UDP

    salve scorrpeio,

    Something looks a bit dubious.
    Are you positive on this line: m_RemoteAdd.sin_port = htonl( INADDR_BROADCAST ); ?
    INADDR_BROADCAST is usually a placeholder for the bcast addr 255.255.255.255.

    You should pass a port number. INADDR_BROADCAST is equivalent to 0xffffffff.
    sin_port being an unsigned short gives you portnumber 65535. Is this your intended usage ?

    Also check the send/receive buffer sizes.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)