您好,登录后才能下订单哦!
DatagramSocket只允许数据报发送给指定的目标地址,而MulticastSocket可以将数据报以广播的方式发送至多个客户端。其主要思想是设置一组特殊网络地址作为多点广播地址,每个多点广播地址都被看做一个组,当客户端需要发送,接收广播消息时,加入到该组即可。
IP协议为多点广播提供了这些特殊的IP地址,这些IP地址的范围是224.0.0.0至239.255.255.255。当MulticastSocket把一个DatagramPacket发送到多点广播IP地址时,该数据将被自动广播到加入该地址的所有MulticastSocket,同时也可以设置该MulticastSocket接收自身发送的数据。
如果仅仅是用于发送数据报的MulticastSocket对象,使用默认地址,随机端口即可。但如果创建接收用的MulticastSocket对象,则该MulticastSocket对象必须指定端口,否则发送方无法确定发送数据报的目标端口。
下面通过一个简单的例子实现多点广播图片:
多点广播的工具类:
public class ComUtil { public static final String BROADCAST_IP = "224.2.2.2"; public static final int BOADCAST_PORT = 30000; private static final int DATA_LEN = 100 * 1024; //定义本程序的MulticastSocket实例 private MulticastSocket socket = null; //定义广播的IP地址 private InetAddress broadcastAddress = null; //定义接收网络数据的字符数组 byte[] inBuff = new byte[DATA_LEN]; //以指定字节数组创建准备接受的DatagramPacket对象 private DatagramPacket inPacket = new DatagramPacket(inBuff , inBuff.length); //定义一个用于发送的DatagramPacket对象 private DatagramPacket outPacket = null; private Handler handler; //构造器,初始化资源 public ComUtil(Handler handler) throws Exception { this.handler = handler; //因为该MultcastSocket对象需要接受数据,所以有指定端口 socket = new MulticastSocket(BOADCAST_PORT); broadcastAddress = InetAddress.getByName(BROADCAST_IP); //将该socket加入指定的多点广播地址 socket.joinGroup(broadcastAddress); //设置本MultcastSocket发送的数据报将被送到本身 socket.setLoopbackMode(false); //初始化发送用的DatagramSocket,它包含一个长度为0的字节数组 outPacket = new DatagramPacket(new byte[0] , 0 , broadcastAddress , BOADCAST_PORT); new ReadBroad().start(); } //广播消息的工具方法 public void broadCast(byte[] msg) { try { //将msg字符串转换为字节数组 byte[] buff = msg; //设置发送用的DatagramPacket里的字节数组 outPacket.setData(buff); //发送数据 socket.send(outPacket); } catch (IOException e) { e.printStackTrace(); } } //持续读取MulticastSocket的线程 class ReadBroad extends Thread { public void run() { while (true) { try { //读取Socket中的数据 socket.receive(inPacket); Message msg = new Message(); msg.what = 0x123; msg.obj = inBuff; handler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); } } } } }
MainActivity类:
public class MainActivity extends Activity { private Button button; private ImageView img; private ComUtil comUitl; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0x123) { byte[] result = (byte[]) msg.obj; img.setImageBitmap(BitmapFactory.decodeByteArray(result , 0 , result.length)); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); try { comUitl = new ComUtil(handler); } catch (Exception e) { e.printStackTrace(); } button = (Button) findViewById(R.id.send_img_all); img = (ImageView) findViewById(R.id.receiver_img); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { sendData(); } }); } private void sendData() { Bitmap bitmap = BitmapFactory.decodeResource(getResources() , R.drawable.wenqing2); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG , 100 , byteArray); bitmap.recycle(); final byte[] msg = byteArray.toByteArray(); new Thread() { @Override public void run() { comUitl.broadCast(msg); } }.start(); try { byteArray.close(); } catch (IOException e) { e.printStackTrace(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。