简体   繁体   中英

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.

System: Linux, Language: C

I am able to bind two sockets in two different processes to the same port using 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.

It is not possible with sockets API, and tcpdump picks packets right from network interface, before any TCP/IP processing.

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

I am currently testing that same behavior with a UDP server.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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