1

I need a couple of C# (one server an a client) routines that allows me to find from a PC, the (WinCE) devices connected to the subnet. Despite testing several examples I am not able to make the broadcasts to work.

Running on the devices there is C# (Compact framework) based server, whose code looks like this:

public void SomeClass()
{

    IPEndPoint ipUDP = new IPEndPoint(IPAddress.Any, 5003);
    this.serverUDP = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    this.serverUDP.Bind(ipUDP);
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 6001);
    this.epSender = (EndPoint)sender;
    this.serverUDP.BeginReceiveFrom(this.bufferUDP, 0, 20, SocketFlags.None, ref epSender, new AsyncCallback(someMethod), null);
}

In the PC, I run a client that should call these servers with a broadcast message:

public static void Discover(ref string mensaje, string IPDestino, ref ArrayList equipos)
{            
    UdpClient client = null;
    try
    {
        client = new UdpClient(6001);

        byte[] toSend = Encoding.ASCII.GetBytes("FINDSTT");
        client.Send(toSend, toSend.Length, "255.255.255.255", 5003);

        //Some logic
    }
    catch (Exception ex)
    {
        //Some handling
    }
    if (client != null)
        client.Close();
}

The problem is that the broadcasting never reach the servers. However if I change in client.Send() the broadcast address (255.255.255.255) for a specific address, it IS received by the server. But this way, I lost the main goal that is to find devices that I don't know beforehand.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ferite
  • 131
  • 1
  • 3
  • 10
  • If you run a packet capture on the server, do you see any of your generated broadcast traffic? (and, I guess more importantly, is the client and serveron the same logical network?) – admdrew Feb 06 '14 at 19:40
  • Is your client running on windows 7? – OnoSendai Feb 06 '14 at 19:45
  • The AsyncCallback (called in the code "someMethod") is never called. It is supposed to be called if some data comes. In this case, the PC and the WinCE devices are connected to the same switch via Ethernet. – Ferite Feb 06 '14 at 19:47
  • @OnoSendai, it is Windows 8 laptop. – Ferite Feb 06 '14 at 19:48
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 06 '14 at 19:51

1 Answers1

0

If your client runs under Windows 7/8, the problem may lie in the way the OS handles the zero network broadcast address 255.255.255.255.

If that's the case, then this post may be of some help to you.

Also, related:

How to fix the global broadcast address (255.255.255.255) behavior on Windows? (Technet, Win7)

IPv4 broadcast problem (win8)

Community
  • 1
  • 1
OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • your aswer resolved half part of the problem. Efectively, the broadcast was not being sent for all the adapters. This can be solved by installing WinIPBroadcast:https://github.com/dechamps/WinIPBroadcast – Ferite Feb 11 '14 at 01:25
  • @OnoSendsai, your answer solved my initial problem. Effectively, the broadcast was not being sent for all the adapters. This can be solved by installing a service that I found in one of your links: https://github.com/dechamps/WinIPBroadcast. – Ferite Feb 11 '14 at 01:32
  • @Ferite that's good to hear. I ran into the same issue in the past, I solved it by programatically mapping the network interfaces and send (here's the pun) individual broadcast packets through each. – OnoSendai Feb 11 '14 at 04:15