Android UDP broadcast Packet crawl

Source: Internet
Author: User

Just paste the code, the previous write suddenly forget.



Private Wifimanager.multicastlock Multicastlock; public void init (context context) {Wifimanager Wifimanager = (wifimanager) context.getsystemservice (context.wifi_s         Ervice);    Multicastlock = Wifimanager.createmulticastlock ("my_net");            } public byte[] Fetchdata () {try {multicastlock.acquire ();            MulticastSocket multicastsocket = new MulticastSocket (55551);            InetAddress Group = inetaddress.getbyname ("224.0.0.2");            Multicastsocket.setsotimeout (3000);            Multicastsocket.joingroup (group);            Multicastsocket.setloopbackmode (TRUE);            byte[] buffer = new BYTE[512];            Datagrampacket packet = new Datagrampacket (buffer, buffer.length);            Multicastsocket.receive (packet);        return buffer;        } catch (Exception e) {e.printstacktrace ();    } finally {multicastlock.release ();//Must be released} return null; }

Permission to join


    <uses-permission android:name= "Android.permission.INTERNET"/>    <uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/>    <uses-permission android:name=" android.permission.ACCESS_ Wifi_state "/>    <uses-permission android:name=" Android.permission.CHANGE_WIFI_MULTICAST_STATE "/>    <uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE"/>    <uses-permission Android:name= "Android.permission.CHANGE_NETWORK_STATE"/>    <uses-permission android:name= " Android.permission.CHANGE_WIFI_MULTICAST_STATE "/>



Android UDP broadcast Packet crawl

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.