2

I'm relatively new to programming C sockets and I have to solve a task in C. There are multiple nodes in the network, each with its own settings. Each node broadcasts its current settings every second. It also has to listen for these broadcasts from other nodes and store their settings too. Finally, it has to be able to send a packet to another node directly. I'm planning to store all node settings in a struct array.

I've managed to finish the broadcast, which is implemented in its own thread, but I'm not sure what is the correct procedure to receive packets from unknown number of other nodes in the network and store their addresses for sending packets to them directly later.

Any tips?

Thanks!

fugasjunior
  • 461
  • 1
  • 6
  • 22
  • This is not a "write my code for me" site. Post what you have tried so far. – Andrew Henle May 14 '18 at 09:18
  • I wasn't really looking for a complete code, more like some suggestions how to procede. So far I've thought about having a separate thread that receives all incoming packets and stores the origin IP in the node settings struct aswell. Would that be an efficient solution? – fugasjunior May 14 '18 at 09:29
  • There is no "receive from specific node". If you bind a socket, you bind it to a local adapter. You get packets from all nodes at that socket. If you want receive via any adapter you bind your socket to INADDR_ANY. – harper May 14 '18 at 11:19
  • You basically receive UDP messages as fast as possible (socket.ReceiveFrom or similar will give you the origin address) and pass them on to your processing logic. – C. Gonzalez May 14 '18 at 18:15

1 Answers1

0

Thanks for all the advice.

In the end, I just chose to compare each incoming packet's origin IP with all the registered units and if no match was found, I added a new one, storing the IP in the unit struct.

fugasjunior
  • 461
  • 1
  • 6
  • 22