简体   繁体   中英

Android Receive UDP broadcast from C# desktop app over LAN?

I'm trying to create a sever application on PC for many android devices using the same wi-fi network. The devices will find the server's IP by receiving UDP broadcast from it contains the server IP data. I've started by creating a sample udp broadcaster in C# and udp receiver in java but I never managed to get the packet on the android side . here is the code :

C#:

UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Broadcast, listenPort);
listener.Connect(groupEP);
listener.EnableBroadcast = true;
byte[] data = new byte[1024];
try
{
    while (!done)
    {
       Console.WriteLine("broadcast");

       Thread.Sleep(400);

       listener.Send(data,2);

     }

Android code :

DatagramSocket socket;
try {
    socket = new DatagramSocket(11000);
    socket.connect(getBroadcastAddress(), 11000);
    socket.setBroadcast(true);
    byte[] buf = new byte[4];
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);

The Internet Permission is set correctly in the manifest. still not able to receive the packets.

Suggestions:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM