[Solved] send network traffic to from a specific ip and port without using recvfrom or accept


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 the SO_BROADCAST option on it, and then sendto() data packets to the broadcast IP address of a given subnet (or use send() if it connect()‘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 use recvfrom() to read the packets (or use recv() if it connect()‘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 use send() if it connect()‘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 use setsockopt() to join the socket to the multicast group (using IP_ADD_MEMBERSHIP for IPv4, and IPV6_ADD_MEMBERSHIP for IPv6), and then use recvfrom() to read the packets (or use recv() if it connect()‘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