简体   繁体   English

UDP单播:两个进程侦听同一个udp端口; 只有一个接收数据包

[英]UDP unicast: two processes listening on same udp port; only one receiving packets

Purpose: to receive UDP unicast packets sent to a single port in two different processes. 目的:接收在两个不同进程中发送到单个端口的UDP单播数据包。

System: Linux, Language: C 系统:Linux,语言:C

I am able to bind two sockets in two different processes to the same port using SO_REUSEADDR. 我可以使用SO_REUSEADDR将两个不同进程中的两个套接字绑定到同一个端口。 But, as expected, the packets are received in only one(the one bound later). 但是,正如预期的那样,数据包只接收一个(稍后绑定的数据包)。

Is it possible to receive packets in both the processes? 是否可以在两个进程中接收数据包? If not, how is tcpdump able to read but not consume packets. 如果没有,tcpdump如何能够读取但不消耗数据包。

It is not possible with sockets API, and tcpdump picks packets right from network interface, before any TCP/IP processing. 使用套接字API是不可能的,并且tcpdump在任何TCP / IP处理之前直接从网络接口中选择数据包。

Your only chance is to receive packets in one process and resend them to another one. 您唯一的机会是在一个进程中接收数据包并将其重新发送到另一个进程。

If you open a socket, bind, listen, then fork() your process, incoming connection requests will be handed, each time, to a randomly selected, single one of those processes (I've never bothered to figure out the strategy that Linux uses because it balances the load well). 如果你打开一个套接字,绑定,监听,然后fork()你的进程,传入的连接请求将每次交给一个随机选择的单个进程(我从来没有费心去弄清楚Linux的策略)使用,因为它很好地平衡了负载)。

I am currently testing that same behavior with a UDP server. 我目前正在使用UDP服务器测试相同的行为。

Why unicast? 为什么要单播? This is exactly what multicast is for. 这正是多播的用途。

To answer you second question - tcpdump gets a copy of each packet it listens to via something called bpf , and that has to be explicitly supported by the network card driver. 为了回答第二个问题 - tcpdump通过名为bpf东西获取它侦听的每个数据包的副本 ,并且必须由网卡驱动程序明确支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM