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

不羁岁月 提交于 2019-12-07 09:05:33

问题


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.


回答1:


Suggestions:

  • Make sure you don't have any firewalls (software or hardware) blocking you

  • Consider using Wireshark:

    http://www.wireshark.org/

  • Look at this example:

    http://code.google.com/p/boxeeremote/wiki/AndroidUDP



来源:https://stackoverflow.com/questions/7784227/android-receive-udp-broadcast-from-c-sharp-desktop-app-over-lan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!