首页 > 移动平台 > 详细

Android UDP广播包抓取

时间:2015-01-04 19:17:32      阅读:337      评论:0      收藏:0      [点我收藏+]

只是贴代码,以前写的突然忘记了.



    private WifiManager.MulticastLock multicastLock;

    public void init(Context context){
         WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
         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();//一定要释放
        }
        return null;
    }

权限加入


    <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广播包抓取

原文:http://blog.csdn.net/birdsaction/article/details/42395191

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!