Android UDP组播示例源代码

发布时间:2017-3-27 4:54:51 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Android UDP组播示例源代码",主要涉及到Android UDP组播示例源代码方面的内容,对于Android UDP组播示例源代码感兴趣的同学可以参考一下。

UDP组播发送服务器源代码: package com.yx.playertest; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.MulticastSocket; import android.app.Activity; import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.MulticastLock; public class TUdpSender { public TUdpSender(WifiManager AWifiManager) { wifiManager = AWifiManager; } public TUdpSender(Activity AContext) { wifiManager = (WifiManager)AContext.getSystemService(AContext.WIFI_SERVICE);; } MulticastLock multicastLock; WifiManager wifiManager; // 单播发送 public void DoSendMsg( String ARemoteIP, int ARemotePort, String AMsg){ DatagramSocket skt= null; allowMulticast(); try { skt = new DatagramSocket(); DatagramPacket sendPgk = new DatagramPacket( AMsg.getBytes(), AMsg.getBytes().length, InetAddress.getByName(ARemoteIP), ARemotePort ); skt.send(sendPgk); } catch(Exception er) { ; } finally { try { skt.close(); } catch(Exception er) { ; } try { multicastLock.release(); } catch(Exception er) { ; } } } // 组播发送 public void DoSendGroupMsg( String AGroupIP, int ARemotePort, String AMsg){ allowMulticast(); MulticastSocket skt= null; try { skt = new MulticastSocket(ARemotePort); InetAddress broadcastAddress = InetAddress.getByName(AGroupIP); skt.joinGroup(broadcastAddress); skt.setLoopbackMode(false); DatagramPacket sendPgk = new DatagramPacket( AMsg.getBytes(), AMsg.getBytes().length, InetAddress.getByName(AGroupIP), ARemotePort ); skt.send(sendPgk); } catch(Exception er) { ; } finally { try { skt.close(); } catch(Exception er) { ; } try { multicastLock.release(); } catch(Exception er) { ; } } } private void allowMulticast(){ //WifiManager wifiManager=(WifiManager)Context.getSystemService(Activity.WIFI_SERVICE); try { multicastLock=wifiManager.createMulticastLock("multicast.test"); multicastLock.acquire(); } catch(Exception er) { ; } } public static void DoSendUdpMsg(Activity AContext ,String ARemoteIP, int ARemotePort, String AMsg) { TUdpSender Audp = new TUdpSender(AContext); Audp.DoSendMsg(ARemoteIP, ARemotePort, AMsg); Audp = null; } public static void DoSendUdpGroupMsg(Activity AContext ,String AGroupIP, int ARemotePort, String AMsg) { TUdpSender Audp = new TUdpSender(AContext); Audp.DoSendGroupMsg(AGroupIP, ARemotePort, AMsg); Audp = null; } } UDP组播接受客户端源代码: package com.yx.playertest; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.TextView; public class TUdpRecevier implements Runnable { private static final String BROADCAST_IP = "230.0.0.1"; // 组播地址 public static final int BROADCAST_PORT = 8800; // 组播端口 private static final int DATA_LEN = 4096; // 最大数据包大小 protected static final String TAG = "MainActivity"; private MulticastSocket socket = null; // MulticastSocket实例 private InetAddress broadcastAddress = null; // Internet地址 byte[] inBuff = new byte[DATA_LEN]; // 接收消息的数组 private DatagramPacket inPacket = new DatagramPacket(inBuff, inBuff.length); // 准备接收数据的DatagramPacket对象 public Boolean IsActived = false; // 接收状态 // public Handler RecMsgHabndler ; //接收到消息后的通知句柄 private void DoInit() { Log.i(TAG, "DoInit-->init"); try { socket = new MulticastSocket(BROADCAST_PORT); broadcastAddress = InetAddress.getByName(BROADCAST_IP); socket.joinGroup(broadcastAddress); socket.setLoopbackMode(false); } catch (Exception er) { ; } finally { ; } } public void DoStart() { if (!IsActived) { try { DoInit(); IsActived = true; new Thread(this).start(); } catch (Exception er) { ; } } } public void DoStop() { Log.i(TAG, "DoStop-->stop"); if (IsActived) { try { IsActived = false; socket.close(); socket = null; } catch (Exception er) { ; } } } public void run() { // TODO Auto-generated method stub while (IsActived) { try { inBuff = new byte[DATA_LEN]; inPacket = new DatagramPacket(inBuff, inBuff.length); Log.i(TAG, "run-->run C "); // 读取Socket中的数据,读到的数据放在inPacket所封装的字节数组里 socket.receive(inPacket); Log.i(TAG, "run-->run D "); String ARecTxt = new String(inBuff, 0, inPacket.getLength()); try { DoOnReceiveMsg(ARecTxt); } catch (Exception er) { String Err = er.getMessage(); int ALen = Err.length(); } } catch (Exception er) { break; } } DoStop(); } public void DoOnReceiveMsg(String ARecMsg) { Message msg = new Message(); Bundle b = new Bundle();// 存放数据 b.putString("RecMsg", ARecMsg); msg.setData(b); this.RecMsgHabndler.sendMessage(msg); } private Handler RecMsgHabndler = new Handler() { @Override public void handleMessage(Message msg) { Log.i(TAG, "HandleMessage-->handleMessage"); Log.i(TAG, msg.getData().getString("RecMsg")); } }; }

上一篇:android中点击viewgroup时,子控件也会变成被点击的状态的原因及解决方法
下一篇:Android支付接入(五):机锋网

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款