Need to continuously send telemetry data over a network. Don’t really care who is going to get it, just need to send it out.
Well, your sender need to know WHERE to send data to.
Typically, a receiver would first send a request to your sender so it knows the receiver exists, and then your sender would know where to send packets to. But, this requires your sender to keep track of all of the receivers so it can send a separate data packet to every receiver individually. Either UDP or TCP can be used for this.
If you don’t want to do things that way, you have 2 other choices:
-
subnet broadcasting (works with IPv4 only) – your sender can create a UDP socket, then use
setsockopt()
to enable theSO_BROADCAST
option on it, and thensendto()
data packets to the broadcast IP address of a given subnet (or usesend()
if itconnect()
‘s to the broadcast IP beforehand). Each packet sent will be automatically delivered to every machine that is connected to that same subnet (whether the machines want the packets or not).Your receiver can then create and
bind()
a UDP socket to a local network interface that is connected to that same subnet, and then userecvfrom()
to read the packets (or userecv()
if itconnect()
‘s to the sender’s IP address beforehand). -
multicasting (works with both IPv4 and IPv6) – your sender can create a UDP socket and then
sendto()
data packets to the IP address of a given multicast group (or usesend()
if itconnect()
‘s to the multicast group IP beforehand). Every packet will be delivered only to receivers who have joined the same group.Your receiver can create and
bind()
a UDP socket to a local network interface that has a network route to the sender, then usesetsockopt()
to join the socket to the multicast group (usingIP_ADD_MEMBERSHIP
for IPv4, andIPV6_ADD_MEMBERSHIP
for IPv6), and then userecvfrom()
to read the packets (or userecv()
if itconnect()
‘s to the sender’s IP beforehand).
All the client needs to do is simply to connect to a specific IP and get data from a specific port and the data will be received by it.
I would suggest using multicasting for this. You get the benefits of being able to send fewer packets on the sender side and have them delivered across the network to (potentially) multiple receivers at the same time, and you reduce network overhead by isolating traffic to only the parties who actually want to receive the packets.
solved send network traffic to from a specific ip and port without using recvfrom or accept