Identify IP Address source of UDP Broadcast C++

Posted on 2011-04-26
Last Modified: 2012-05-11
Dear Experts.

I have the following code excerpt (for illustrative purposes) and would like you to give me an example of the following:

1: Use a UDP Broadcast to output the IP address of the server that it is sent from.
2: Use this IP address to connect to said server via TCP from the client's perspective.

I attempted to write this code to output the IP address, and parse it into the variable m_szSendBuf to be sent over the network - (so that I could remote connect to this server).

cout << "1 Type a message to send (maximum 100 characters)" << endl;
      cin      >> m_szSendBuf;
      cout << "The message you have typed is: " << m_szSendBuf << endl << endl;

      cout << "Windows Socket mechanism initialised" << endl;
      cout << "Socket created" << endl;
      cout << "Broadcast Option set" << endl;
      cout << "Broadcast Address and Port set (port " << htons(m_SendSockAddr.sin_port) << " will be used)" << endl;

      printf("The local sending IP address is %d.%d.%d.%d\n",
      cout << "Datagram sent" << endl;

      unsigned int i_octet1;
      unsigned int i_octet2;
      unsigned int i_octet3;
      unsigned int i_octet4;

      string s_octet1;
      string s_octet2;
      string s_octet3;
      string s_octet4;
      i_octet1 = m_LocalSockAddr.sin_addr.S_un.S_un_b.s_b1;
      i_octet2 = m_LocalSockAddr.sin_addr.S_un.S_un_b.s_b2;
      i_octet3 = m_LocalSockAddr.sin_addr.S_un.S_un_b.s_b3;
      i_octet4 = m_LocalSockAddr.sin_addr.S_un.S_un_b.s_b4;

      stringstream s_octet1;
      //int i;
       s_octet1 << i_octet1;
       const char* p = s_octet1.str().c_str();
      cout << m_szSendBuf << endl;
      cout << i_octet1 << endl;
      cout << s_octet1 << endl;
      cout << p << endl;

Thanks in advance for taking the time to look at my code.

Regards, SME_Paddington
Question by:SME_Paddington
    LVL 86

    Accepted Solution

    Have you tried 'recvfrom()' ( on the receiving side? E.g.
    struct sockaddr_in from;
    socklen_t fromlen;
    fromlen = sizeof(struct sockaddr_in);
    rc = recvfrom(s, buf, len, flags, (struct sockaddr *) &from,

    Open in new window


    Author Comment

    Will give it a shot, and let you know what happens. Thanks for the tip.

    Author Closing Comment

    Good guy, knows his stuff.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now