Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android UDP廣播包抓取

Android UDP廣播包抓取

編輯:關於Android編程

只是貼代碼,以前寫的突然忘記了.

 

 

 


    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;
    }


 

 

 

    
    
    
    
    
    
    



 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved