码迷,mamicode.com
首页 > 移动开发 > 详细

Android对UDP组播接收数据的有限支持(一)

时间:2017-06-06 20:47:25      阅读:645      评论:0      收藏:0      [点我收藏+]

标签:img   exception   java.net   技术分享   pmu   thread   amp   nts   分代   

  最近,想做一个跨平台的局域网的文件传输软件,思路是组播设备信息,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             @Override
31             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     @Override
17     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的封杀极为严重,折腾开始了

Android对UDP组播接收数据的有限支持(一)

标签:img   exception   java.net   技术分享   pmu   thread   amp   nts   分代   

原文地址:http://www.cnblogs.com/Ralap/p/Android_udp_multcast_support.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!