Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

c - send network traffic to from a specific ip and port without using recvfrom or accept

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...