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:
Make sure you don't have any firewalls (software or hardware) blocking you
Consider using Wireshark:
Look at this example: