41

I've got 2 programs, 1 for sending an UDP broadcast message and 1 that is listening for this broadcast. My problem is that sometimes when I send a broadcast, the receiver receives 2 messages. Why?

Receiver code:

public class Receiver {
  private readonly UdpClient udp = new UdpClient(15000);
  private void StartListening()
  {
    this.udp.BeginReceive(Receive, new object());
  }
  private void Receive(IAsyncResult ar)
  {
    IPEndPoint ip = new IPEndPoint(IPAddress.Any, 15000);
    byte[] bytes = udp.EndReceive(ar, ref ip);
    string message = Encoding.ASCII.GetString(bytes);
    StartListening();
  }
}

Sender code:

public class Sender {
  public void Send() {
    UdpClient client = new UdpClient();
    IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 15000);
    byte[] bytes = Encoding.ASCII.GetBytes("Foo");
    client.Send(bytes, bytes.Length, ip);
    client.Close();
  }
}
Oliver
  • 43,366
  • 8
  • 94
  • 151
Paw Baltzersen
  • 2,662
  • 3
  • 24
  • 33
  • hi. I wanted to use your code for UDP file transferring. but i don't know what to do with the Receive part! how should it be implemented? how it's going to begin getting stuff from network? – Shamim Dec 29 '13 at 07:09
  • @Shamim, there are plenty of resources on the web, a bit of googling should help. Look for server/client examples. – Paw Baltzersen Dec 29 '13 at 14:41
  • `IPEndPoint ip = new IPEndPoint(IPAddress.Any, 15000);` can just be `IPEndPoint ip = null;` - it's not a struct. Its use in the `EndReceive()` method is to define the sender. Populating it with anything at all is a bit misleading. – maxp Jan 25 '15 at 11:19
  • Your question really helped as an answer, since I'm doing loopback udp never loss =) thank you – Val Nov 21 '17 at 05:18
  • After couple of hours it seems this code crush and give error "An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full" Any idea why ? – Alophind Nov 21 '17 at 16:23
  • @Alophind This code is so old I don't think I'd code it this way today. I'd probably use a framework instead to abstract it away. – Paw Baltzersen Dec 11 '17 at 08:27
  • @PawBaltzersen Do you mean UDPClient ? I'm trying send and listen to UDP messages between couple of computers but I don't want each computer to receive its own message , only the others , I've tried disabling the loopback but without success. – Alophind Dec 11 '17 at 08:59

4 Answers4

33

Strictly speaking, packet duplication in IP network is allowed behavior of the network and you have to be able to deal with it in your software even if you will somehow get rid of it this time. If you are just wondering about why this happens in your particular case... at a first glance I see nothing wrong with your code. Do you have several IP addresses on Ethernet port of your computer or some such? I think wireshark might help get more details about what's going on.

Eugene Ryabtsev
  • 2,232
  • 1
  • 23
  • 37
  • 9
    Your question answered it. My laptop's wireless was turned on while I was connected through cable as well. So I received the package through the cable and wireless. Thank you. – Paw Baltzersen May 31 '12 at 11:59
6

UDP packets aren't reliable, it's totally possible that you'll get the same packet twice or even none at all, when using udp you need to include some kind of unique ID in your data so you can discard errors or request a resend.

Andy
  • 6,366
  • 1
  • 32
  • 37
1

The reason is when you broadcast you send your message to all the end points in the network. Since you are in the same network, you will receive the same message because it is broadcasted. You can write a custom filter for that.

Shiran Abbasi
  • 89
  • 1
  • 2
  • 7
1

Keep in mind that (1) UDP packets are VERY reliable for all clients on the same router, and (2) packet duplication can occur when more than one path is available from the server to the client.

I had this problem on a VM, and it was solved by a network guru smarter than me who added a virtual NIC to the VM and had me run "route delete" and "route add" commands.

Specifically (on Windows 10 VM), if the new virtual NIC is 10.10.10.10: route delete 224.0.0.0 mask 240.0.0.0 route add 224.0.0.0 mask 240.0.0.0 10.10.10.10

Ted W
  • 239
  • 4
  • 11