亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播圖片

 更新時(shí)間:2019年01月05日 15:48:24   作者:Knick_Zhang  
這篇文章主要為大家詳細(xì)介紹了Android使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

DatagramSocket只允許數(shù)據(jù)報(bào)發(fā)送給指定的目標(biāo)地址,而MulticastSocket可以將數(shù)據(jù)報(bào)以廣播的方式發(fā)送至多個(gè)客戶(hù)端。其主要思想是設(shè)置一組特殊網(wǎng)絡(luò)地址作為多點(diǎn)廣播地址,每個(gè)多點(diǎn)廣播地址都被看做一個(gè)組,當(dāng)客戶(hù)端需要發(fā)送,接收廣播消息時(shí),加入到該組即可。

IP協(xié)議為多點(diǎn)廣播提供了這些特殊的IP地址,這些IP地址的范圍是224.0.0.0至239.255.255.255。當(dāng)MulticastSocket把一個(gè)DatagramPacket發(fā)送到多點(diǎn)廣播IP地址時(shí),該數(shù)據(jù)將被自動(dòng)廣播到加入該地址的所有MulticastSocket,同時(shí)也可以設(shè)置該MulticastSocket接收自身發(fā)送的數(shù)據(jù)。

如果僅僅是用于發(fā)送數(shù)據(jù)報(bào)的MulticastSocket對(duì)象,使用默認(rèn)地址,隨機(jī)端口即可。但如果創(chuàng)建接收用的MulticastSocket對(duì)象,則該MulticastSocket對(duì)象必須指定端口,否則發(fā)送方無(wú)法確定發(fā)送數(shù)據(jù)報(bào)的目標(biāo)端口。

下面通過(guò)一個(gè)簡(jiǎn)單的例子實(shí)現(xiàn)多點(diǎn)廣播圖片:

多點(diǎn)廣播的工具類(lèi):

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實(shí)例
 private MulticastSocket socket = null;
 //定義廣播的IP地址
 private InetAddress broadcastAddress = null;
 //定義接收網(wǎng)絡(luò)數(shù)據(jù)的字符數(shù)組
 byte[] inBuff = new byte[DATA_LEN];
 //以指定字節(jié)數(shù)組創(chuàng)建準(zhǔn)備接受的DatagramPacket對(duì)象
 private DatagramPacket inPacket = new DatagramPacket(inBuff , inBuff.length);
 //定義一個(gè)用于發(fā)送的DatagramPacket對(duì)象
 private DatagramPacket outPacket = null;
 private Handler handler;

 //構(gòu)造器,初始化資源
 public ComUtil(Handler handler) throws Exception
 {
 this.handler = handler;
 //因?yàn)樵揗ultcastSocket對(duì)象需要接受數(shù)據(jù),所以有指定端口
 socket = new MulticastSocket(BOADCAST_PORT);
 broadcastAddress = InetAddress.getByName(BROADCAST_IP);
 //將該socket加入指定的多點(diǎn)廣播地址
 socket.joinGroup(broadcastAddress);
 //設(shè)置本MultcastSocket發(fā)送的數(shù)據(jù)報(bào)將被送到本身
 socket.setLoopbackMode(false);
 //初始化發(fā)送用的DatagramSocket,它包含一個(gè)長(zhǎng)度為0的字節(jié)數(shù)組
 outPacket = new DatagramPacket(new byte[0] , 0 , broadcastAddress , BOADCAST_PORT);
 new ReadBroad().start();
 }

 //廣播消息的工具方法
 public void broadCast(byte[] msg)
 {
 try
 {
  //將msg字符串轉(zhuǎn)換為字節(jié)數(shù)組
  byte[] buff = msg;
  //設(shè)置發(fā)送用的DatagramPacket里的字節(jié)數(shù)組
  outPacket.setData(buff);
  //發(fā)送數(shù)據(jù)
  socket.send(outPacket);
 }
 catch (IOException e)
 {
  e.printStackTrace();
 }
 }

 //持續(xù)讀取MulticastSocket的線(xiàn)程
 class ReadBroad extends Thread
 {
 public void run()
 {
  while (true)
  {
  try
  {
   //讀取Socket中的數(shù)據(jù)
   socket.receive(inPacket);
   Message msg = new Message();
   msg.what = 0x123;
   msg.obj = inBuff;
   handler.sendMessage(msg);
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }
  }
 }
 }
}

MainActivity類(lèi):

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論