send network traffic to from a specific ip and por

2019-09-25 10:33发布

问题:

I'm new to sockets and network programming so if the question seems dumb, that's because I don't fully understand the above mentioned topics.

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. 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.

My code is written in C.

Originally I thought it would be a UDP socket. But it has recvfrom method, i.e. I (server) need to wait till someone connects to it. Then I decided to look at TCP/IP socket but this one has an accept method.

I looked online for quite some time but didn't find any code that could help me out (maybe I was looking in a wrong place).

Does anyone know if what I'm talking about is possible to do? If so how would I do it? If no, then are there any other ways to do it, i.e. by not using sockets?

回答1:

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.



回答2:

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.

So use UDP.

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.

So use UDP. Just call sendto() whenever necessary.

Originally I thought it would be a UDP socket.

You were correct.

But it has recvfrom() method

Correct.

i.e. I (server) need to wait till someone connects to it.

Incorrect. recvfrom() waits until somebody sends to it, and the sender doesn't have to wait at all.

Then I decided to look at TCP/IP socket but this one has an accept() method.

Correct. You would have to make your sender a server, use listen(), accept(), send(), etc. I don't recommend this for your stated need.

If you want to have multiple listeners, use UDP multicast.