Android對UDP組播接收資料的有限支援(一),android組播

來源:互聯網
上載者:User

Android對UDP組播接收資料的有限支援(一),android組播

  最近,想做一個跨平台的區域網路的檔案傳輸軟體,思路是組播裝置資訊,TCP串連傳輸檔案。於是進行了一次簡單的UDP組播測試,發現Android對於UDP組播接收資料的支援即極為有限。

部分代碼如下

 1 package com.hsocket.Udp; 2  3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6  7 public class UdpReceiver { 8     protected DatagramSocket client=null; 9     private OnReceiveListener mOnReceiveListener=null;10     private Thread thrRecv=null;11     protected int port=0;12     public UdpReceiver(int port){13         this.port=port;14     }15     protected DatagramSocket Create() throws IOException{16         return new DatagramSocket(this.port);17     }18     public void addOnReceiveListener(OnReceiveListener mOnReceiveListener){19         this.mOnReceiveListener=mOnReceiveListener;20     }21     public void Stop(){22         if(this.thrRecv!=null) this.thrRecv.interrupt();23         this.Close();24     }25     public void Listen() throws IOException{26         this.Close();27         this.client=this.Create();28         if(this.thrRecv!=null) this.thrRecv.interrupt();29         this.thrRecv=new Thread(new Runnable() {30             @Override31             public void run() {32                 while(!Thread.interrupted()){33                     ReceiveEventArgs args=new ReceiveEventArgs();34                     try {35                         DatagramPacket packet=UdpReceiver.this.Receive();36                         args.Address=packet.getAddress();37                         args.Result=packet.getData();38                         args.Length=packet.getLength();39                         args.Error=false;40                     } catch (IOException e) {41                         e.printStackTrace();42                         args.Exception=e;43                         args.Error=true;44                     }45                     UdpReceiver.this.OnReceive(args);46                     if(UdpReceiver.this.mOnReceiveListener!=null)47                         UdpReceiver.this.mOnReceiveListener.OnReceive(UdpReceiver.this, args);48                 }49             }50         });51         this.thrRecv.start();52     }53     protected DatagramPacket Receive() throws IOException{54         byte[] recvBuf = new byte[4096];55         DatagramPacket recvPacket= new DatagramPacket(recvBuf , recvBuf.length);56         this.client.receive(recvPacket);57         return recvPacket;58     }59     protected void Close(){60         if(this.client!=null) this.client.close();61     }62     protected void OnReceive(ReceiveEventArgs args){63         64     }65 }
View Code
 1 package com.hsocket.Udp; 2  3 import java.io.IOException; 4 import java.net.DatagramSocket; 5 import java.net.InetAddress; 6 import java.net.MulticastSocket; 7  8 public class UdpMultcastReceiver extends UdpReceiver { 9 10     private InetAddress multicastAddr=null;11     public UdpMultcastReceiver(InetAddress multicastAddr,int port) {12         super(port);13         this.multicastAddr=multicastAddr;14     }15 16     @Override17     protected DatagramSocket Create() throws IOException {18         MulticastSocket socket=new MulticastSocket(this.port);19         socket.joinGroup(this.multicastAddr);20         socket.setLoopbackMode(false);21         return socket;22     }23 }
View Code

  發現UDP組播接收資料在部分機型存在檔案,與系統有極大關係。小米、華為的手機的深度定製系統對UDP的封殺極為嚴重,折騰開始了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.