不使用 recvfrom 或 accept 从特定 ip 和端口发送网络流量 [英] send network traffic to from a specific ip and port without using recvfrom or accept

查看:60
本文介绍了不使用 recvfrom 或 accept 从特定 ip 和端口发送网络流量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是套接字和网络编程的新手,所以如果这个问题看起来很愚蠢,那是因为我没有完全理解上述主题.

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.

需要通过网络持续发送遥测数据.真的不在乎谁会得到它,只需要发送它.客户端所需要做的只是简单地连接到特定 IP 并从特定端口获取数据,然后数据就会被它接收.

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.

我的代码是用C编写的.

本来以为是UDP套接字.但是它有 recvfrom 方法,即我(服务器)需要等到有人连接到它.然后我决定看看 TCP/IP 套接字,但这个有一个 accept 方法.

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

有谁知道我所说的是否可行?如果是这样,我会怎么做?如果不是,那么还有其他方法可以做到这一点,即不使用 sockets 吗?

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?

推荐答案

需要通过网络持续发送遥测数据.真的不在乎谁会得到它,只需要把它发送出去.

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.

通常,接收方会首先向您的发送方发送请求,以便它知道接收方存在,然后您的发送方将知道将数据包发送到何处.但是,这需要您的发送方跟踪所有接收方,以便它可以单独向每个接收方发送单独的数据包.UDP 或 TCP 均可用于此目的.

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:

  • 子网广播(仅适用于 IPv4) - 您的发件人可以创建一个 UDP 套接字,然后使用 setsockopt() 启用 SO_BROADCAST 选项就可以了,然后sendto() 数据包到给定子网的广播 IP 地址(或者使用 send() 如果它connect() 的广播IP).发送的每个数据包将自动传送到每台连接到同一子网的机器(无论机器是否需要数据包).

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

然后您的接收者可以创建一个 UDP 套接字并将 bind() 连接到连接到同一子网的本地网络接口,然后使用 recvfrom() 读取数据包(或使用 recv() 如果它预先connect() 到发送者的 IP 地址).

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

多播(适用于 IPv4 和 IPv6) - 您的发送方可以创建一个 UDP 套接字,然后 sendto() 数据包到一个 IP 地址给定的多播组(如果 connect() 事先连接到多播组 IP,则使用 send()).每个数据包将发送给加入同一组的接收者.

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.

您的接收方可以创建一个 UDP 套接字并将其 bind() 连接到具有到发送方的网络路由的本地网络接口,然后使用 setsockopt() 加入套接字连接到多播组(IPv4 使用 IP_ADD_MEMBERSHIP,IPv6 使用 IPV6_ADD_MEMBERSHIP),然后使用 recvfrom() 读取数据包(或如果 connect() 事先连接到发件人的 IP,则使用 recv().

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

客户端需要做的只是简单地连接到特定的 IP 并从特定的端口获取数据,然后数据就会被它接收.

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.

这篇关于不使用 recvfrom 或 accept 从特定 ip 和端口发送网络流量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆