0

I want to use sockets (Dgram/UDP) for interprocess communication. The goal is to write 2 programs - one should send data (called sender), one should receive it (called receiver). Since I'm on the same machine, I'm using the loopback adapter, port 11000 (taken from MSDN Socket.ReceiveFrom example).

Relevant parts of sender

// Create
var endPoint = new IPEndPoint(IPAddress.IPv6Loopback, 11000);
var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
// Send
socket.SendTo(buffer, endPoint); // buffer is byte[4]
// Dispose
socket.Close();

Relevant parts of receiver

// Create
var endPoint = new IPEndPoint(IPAddress.IPv6Loopback, 11000);
var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)
var sender = new IPEndPoint(IPAddress.IPv6Loopback, 11000);
var senderRemote = (EndPoint) sender;
socket.Bind(endPoint);
// Loop
socket.ReceiveFrom(buffer, 4, SocketFlags.None, ref senderRemote); // buffer is byte[4]
// Finally
socket.Close();

It's no problem to launch two instances of the sender and send data from both applications. All packets are received in the receiver. But I can only launch one instance of the receiver, because the end point is already bound.

I guess I'm understanding something wrong, but I thought the very idea of UDP was to allow for multiple receivers rather than multiple senders?

Why does doing the "weird" thing (multiple senders) work but the "obvious" thing (multiple receivers) does not? Where am I going wrong, and what do I need to change to allow for multiple receivers?

LWChris
  • 3,320
  • 1
  • 22
  • 39
  • 2
    The idea of UDP has nothing to do with multiple senders or receivers. It is connectionless, which means you don't have to connect, and you can send any packet to any address at any time... that doesn't mean you can use the same address twice. – user253751 Jun 14 '22 at 18:09
  • Possible duplicate of https://stackoverflow.com/questions/22810511/bind-multiple-listener-to-the-same-port – user253751 Jun 14 '22 at 18:10
  • 1
    "*I thought the very idea of UDP was to allow for multiple receivers rather than multiple senders?*" - You are probably thinking of **broadcasting/multicasting**, where 1 packet is sent to 1 address, and then multiple receivers will receive the same packet. – Remy Lebeau Jun 14 '22 at 18:42
  • @RemyLebeau Yeah, from the dupe suggestion I figure that's what I'm thinking about. The code there uses `UdpClient` - using the multicast address with `Socket` for `SendTo` works but not for `Bind` in receiver. I also found MSDN example for `Socket` with multicast, but that code requires the user to enter the `local IP address` which I don't understand the implications of yet. But I only had time to tinker with it for 10 minutes, so maybe it'll clear up. – LWChris Jun 14 '22 at 23:11
  • 1
    @LWChris no, you cannot `bind` to a multicast IP, you have to *join* the group it represents. That is a separate command. You `bind` to the IP of the local adapter that is joining the group. – Remy Lebeau Jun 15 '22 at 00:24
  • 1
    You want `UdpClient.JoinMulticastGroup` and the sender needs to send on a multicast IP address – Charlieface Jun 15 '22 at 08:17
  • @RemyLebeau ah, so that's what I need the local address for. Joining the multicast group essentially tells the network interface to duplicate all traffic that arrives on the group address and forward that to the local address my program has bound to and listens. Correct? – LWChris Jun 16 '22 at 10:58
  • 1
    @LWChris that is correct – Remy Lebeau Jun 16 '22 at 14:34

0 Answers0