Mr.Me
Mr.Me

Reputation: 9296

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.

Upvotes: 4

Views: 7114

Answers (1)

paulsm4
paulsm4

Reputation: 121881

Suggestions:

Upvotes: 2

Related Questions