epandit
epandit

Reputation: 53

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.

Upvotes: 5

Views: 5705

Answers (3)

Walt Howard
Walt Howard

Reputation: 8218

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.

Upvotes: 1

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

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.

Upvotes: 0

blaze
blaze

Reputation: 4364

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.

Upvotes: 2

Related Questions