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.

有帮助吗?

解决方案

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)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top