?
Solved

Identify IP Address source of UDP Broadcast C++

Posted on 2011-04-26
3
Medium Priority
?
760 Views
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;

      InitialiseWindowsSocketsMechanism();
      cout << "Windows Socket mechanism initialised" << endl;
      CreateSocket();
      cout << "Socket created" << endl;
      SetBroadcastOption();
      cout << "Broadcast Option set" << endl;
      SetUpSendAddressStructForBroadcast();
      SetUpLocalAddressStruct();
      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",
      m_LocalSockAddr.sin_addr.S_un.S_un_b.s_b1,
      m_LocalSockAddr.sin_addr.S_un.S_un_b.s_b2,
      m_LocalSockAddr.sin_addr.S_un.S_un_b.s_b3,
      m_LocalSockAddr.sin_addr.S_un.S_un_b.s_b4);
      
      SendMessage();
      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;
      printf("%d",);

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

Regards, SME_Paddington
0
Comment
Question by:SME_Paddington
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 35467359
Have you tried 'recvfrom()' (http://msdn.microsoft.com/en-us/library/ms740120(VS.85).aspx) 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,
&fromlen);

Open in new window

0
 

Author Comment

by:SME_Paddington
ID: 35468814
Will give it a shot, and let you know what happens. Thanks for the tip.
0
 

Author Closing Comment

by:SME_Paddington
ID: 35469726
Good guy, knows his stuff.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

850 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