473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Extracting the responding IP adress from a Broadcast transmission (over UDP socket)

Sagittarius
Hi there.

I have a problem concerning an UDP socket in C++ (Winsock).

The next paragraphs is merely to explain the system I am working on. If U want to skip it, I have marked the question in bold below it.

I have a pc, connected to a network of boards, all connected in a ring, all having their own unique IP adresses.
As it is now, I send messages to the network of boards via broadcast, so all the boards gets the message.
All the boards looks at the message, and if they are'nt the reciever of the message, they just passes it to the next board in the ring.
One of the boards is connected directly to the PC, and is therefore acting as the Gateway-board - and that is exactly the core of my question:

I want only the first message to be sent over broadcast, since the response wil come from the gateway-board. All the following messages only needs to be sent to the gateway-board. If the message is not for the gateway board, it will just pass it along to the next board, which will do the same, until the message reaches the desired board. Here a response will be generated, and a message will be send back trough the network, ending at the gateway-board, which will send it to the PC.

All this was just background - my question is fairly simple (but for the , the answer is'nt):

The fist message I send over broadcast, will recieve response from the gateway-board. How do I in C++ extract the IP adress from that response - the IP adress of the Gateway-board?
If I can get the IP adress, I can store it, and make it so that all other messages are sent to that adress, instead of broadcast.

I am using UDP (Winsock) socket in C++. I dont know is there is a predefines funtion to extract the IP adress from a UDP socket response

I hope that someone is able to help.

Best regards and thanks in advance.

/Sagi

P.S. I dont know if it helps, but il post my code here, so U can see it:

Expand|Select|Wrap|Line Numbers
  1. WSADATA ws;
  2.  WSAStartup(0x0101,&ws);
  3.  
  4.           struct sockaddr_in peer;
  5.           int peerlen = 0;
  6.  
  7.           peer.sin_family = AF_INET;
  8.           peer.sin_port = htons(8998); //port
  9.           peer.sin_addr.s_addr = inet_addr("192.168.34.255"); //Broadcast IP adress
  10.  
  11.     udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
  12.  
  13.      sendto(udp_socket, RISP_Frame, 10, 0, (struct sockaddr *)&peer, sizeof(peer));
  14.  
  15.           //The buffer array needs to be empty
  16.           char recvbuffer[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  17.           int retval = 0;
  18.  
  19.           peer.sin_family = AF_INET;
  20.           peer.sin_port = htons(1228);
  21.           peer.sin_addr.s_addr = htonl(INADDR_ANY);
  22.  
  23.           udp_socket_rec = socket(AF_INET, SOCK_DGRAM, 0);
  24.  
  25.           bind(udp_socket_rec,(struct sockaddr *)&peer,sizeof(peer));
  26.  
  27.  
  28.           FD_ZERO(&readSet);
  29.           FD_SET(udp_socket_rec, &readSet);
  30.           timeVal.tv_sec = 1;
  31.           timeVal.tv_usec = 0;
  32.  
  33.           for (int tries =0; tries <= 2; tries ++) //3 tries before transmitting is aborted
  34.           {
  35.  
  36.           if (select(udp_socket_rec, &readSet, NULL, NULL, &timeVal)) //Checks if there is something in the socket
  37.              {
  38.              peerlen=sizeof(peer);
  39.              retval = recvfrom(udp_socket_rec, recvbuffer, sizeof(recvbuffer), 0, (struct sockaddr *)&peer, &peerlen);
  40.              closesocket(udp_socket_rec);
  41.  
  42.             /* bla bla bla */
  43.  
  44.              break;
  45.              }
  46.           else
  47.           {
  48.               cout << "Retransmitting..." << endl;
  49.  
  50.               closesocket(udp_socket);
  51.  
  52.               /* A new socketconnection is established and so an and so forth */
  53.  
Mar 1 '07 #1
3 3632
In addition to my question above, I would also like to know, if there is a C++ command or function that will return the machines local IP and Subnet.

Thanks in advance.

/Sagi
Mar 2 '07 #2
lqdeffx
39
in your recvfrom(...) function the first parameter is the socket structure of the node that is sending you data. so to get the IP address from that structure, you could use
Expand|Select|Wrap|Line Numbers
  1.  char* inet_ntoa(udp_socket_rec.sin_addr) 
to get the dotted version. shows an example also if you do a google search for socket api it should list several other functions you could use.
Mar 2 '07 #3
Unfortunately, I still have trouble making it work...

Im am sorry, but I am not the most experienced socket programmer.

Could you provide slightly more detail?

I really appreciate the help, and I am sorry for being awkward.

All the best

/Sagi
Mar 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: RosalieM | last post by:
How can i know the nic adress from an incoming socket ? I can easily know the ip adress and port number but how do i have some information about arp level ? Thanks
2
by: | last post by:
I'm sort of new to both Python and socket programming so I appologize ahead of time if this is a dumb question. I have found that the following code works on windows but on linux I get an exception....
0
by: Stephan Steiner | last post by:
Hi The project I'm currently working on involves sending large UDP broadcasts. As the .NET framework already provides an easy facility for sending and receiving UDP packets I thought it was a...
6
by: pekspro | last post by:
I need some code that gets the address from a server. I read somewhere that you could do this by starting some broadcast server using UDP. The client should send an broadcast message, and when the...
8
by: Frank Esser | last post by:
Hello! I have got machines that answer a certain UDP broadcast request with certain information about them. Some years ago I wrote a VB6 application that just sent out this UDP broadcast...
2
by: Gunnar_Frenzel | last post by:
Hello, this might be an easy question, but I don't have any handy solution at hand. I have an application that is supposed to send UDP broadcast. So far so easy, I did: Socket sockSendBroadcast...
8
by: swell | last post by:
I would like to write a server with the low level API of python ( socket+select and/or socket+thread ) that allow me to register client and update them every X seconds ( could be the time, the...
1
by: Larry.Martell | last post by:
I am trying to send UDP broadcast packets over a specific interface and I am having trouble specifying the interface: host='192.168.28.255' sock = socket.socket(socket.AF_INET,...
2
by: OBones | last post by:
Hi all, I'm currently trying to receive replies to a UDP broadcast that I sent and I can't get it to work. I know the datagrams are sent as I see them with WireShark, but my C# code does not see...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.