2

I am sending directed broadcast packets from a UdpClient.

I am also listening on the target port on a second UdpClient (in the same app).

I do not want to see packets created by myself, so I thought it would just be a case of setting MulticastLoopback to false, but this does not work. I still see my own packets.

 private readonly IPAddress BroadcastAddress = IPAddress.Parse("169.254.255.255");
 private const int Port = 0x88a4;

 private UdpClient _udpSend, _udpReceive;

 private void InitUdp()
 {
     _udpSend = new UdpClient();
     _udpSend.EnableBroadcast = true;
     _udpSend.MulticastLoopback = false;
     _udpSend.Connect(BroadcastAddress, Port);

     _udpReceive = new UdpClient(new IPEndPoint(IPAddress.Any, Port));
     _udpReceive.MulticastLoopback = false;
     _udpReceive.BeginReceive(OnUdpPacket, null);           
 }

Any ideas anyone?

Am I able to achieve this with UdpClient or do I have to drop back to raw Sockets instead?

EDIT:

I've tried using a single UdpClient instance for both sending and recieving, but now I never receive any data, regardless of the MulticastLoopback setting!

I have tried listening on both IPAddress.Any, and my local IP, but I never see any packets.

 private readonly IPAddress BroadcastAddress = IPAddress.Parse("169.254.255.255");
 private const int Port = 0x88a4;

 private UdpClient _udpClient;

 private void InitUdp()
 {
     _udpClient= new UdpClient(new IPEndPoint(IPAddress.Any, Port));
     _udpClient.Connect(BroadcastAddress, Port);
     _udpClient.BeginReceive(OnUdpPacket, null);           
 }
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
  • any reason you bind to `IPAddress.Any` ? – Yahia Jan 10 '12 at 11:51
  • I tried binding to BroadcastAddress but I get an Exception "The requested address is not valid in its context". Googling seemed to indicate that this was normal. – GazTheDestroyer Jan 10 '12 at 11:53
  • I am not suggesting binding to broadcast but to a specific IP address... – Yahia Jan 10 '12 at 11:55
  • I need to be able to respond to packets broadcast from any other machine, so can't use a specific IP. – GazTheDestroyer Jan 10 '12 at 11:58
  • Using a specific on the receiving side allows you to receive packets from any machine - as long as your IP is either directly targeted or part of the according broadcast-group. – Yahia Jan 10 '12 at 12:02
  • Thanks for the info, but just tried listening on my local IP only, and still seeing the packets I'm broadcasting from _udpSend. :( – GazTheDestroyer Jan 10 '12 at 12:07
  • which OS are you using ? – Yahia Jan 10 '12 at 12:15
  • How is your firewall etc. configured ? – Yahia Jan 10 '12 at 12:45
  • Firewall is fine. I can see the packets on the wire with Wireshark, I just don't want to see them in my code! :-) – GazTheDestroyer Jan 10 '12 at 13:05
  • I think the property affects whether you receive packets sent *on that instance*, not about two different instances on the same machine (which could, after all, be in different processes, running under different accounts) – Damien_The_Unbeliever Jan 10 '12 at 13:19
  • So can I send and receive on the same instance? I tried to do this originally but it wouldn't work. Probably because of the exception I was getting above. I will have another go. – GazTheDestroyer Jan 10 '12 at 13:23

0 Answers0