DatagramSocket只允許數(shù)據(jù)報發(fā)送給指定的目標(biāo)地址,而MulticastSocket可以將數(shù)據(jù)報以廣播的方式發(fā)送至多個客戶端。其主要思想是設(shè)置一組特殊網(wǎng)絡(luò)地址作為多點廣播地址,每個多點廣播地址都被看做一個組,當(dāng)客戶端需要發(fā)送,接收廣播消息時,加入到該組即可。
網(wǎng)站設(shè)計制作、成都網(wǎng)站建設(shè)的關(guān)注點不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給創(chuàng)新互聯(lián)一個展示的機會來證明自己,這并不會花費您太多時間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗,一切以用戶為中心。
IP協(xié)議為多點廣播提供了這些特殊的IP地址,這些IP地址的范圍是224.0.0.0至239.255.255.255。當(dāng)MulticastSocket把一個DatagramPacket發(fā)送到多點廣播IP地址時,該數(shù)據(jù)將被自動廣播到加入該地址的所有MulticastSocket,同時也可以設(shè)置該MulticastSocket接收自身發(fā)送的數(shù)據(jù)。
如果僅僅是用于發(fā)送數(shù)據(jù)報的MulticastSocket對象,使用默認地址,隨機端口即可。但如果創(chuàng)建接收用的MulticastSocket對象,則該MulticastSocket對象必須指定端口,否則發(fā)送方無法確定發(fā)送數(shù)據(jù)報的目標(biāo)端口。
下面通過一個簡單的例子實現(xiàn)多點廣播圖片:
多點廣播的工具類:
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; //定義接收網(wǎng)絡(luò)數(shù)據(jù)的字符數(shù)組 byte[] inBuff = new byte[DATA_LEN]; //以指定字節(jié)數(shù)組創(chuàng)建準(zhǔn)備接受的DatagramPacket對象 private DatagramPacket inPacket = new DatagramPacket(inBuff , inBuff.length); //定義一個用于發(fā)送的DatagramPacket對象 private DatagramPacket outPacket = null; private Handler handler; //構(gòu)造器,初始化資源 public ComUtil(Handler handler) throws Exception { this.handler = handler; //因為該MultcastSocket對象需要接受數(shù)據(jù),所以有指定端口 socket = new MulticastSocket(BOADCAST_PORT); broadcastAddress = InetAddress.getByName(BROADCAST_IP); //將該socket加入指定的多點廣播地址 socket.joinGroup(broadcastAddress); //設(shè)置本MultcastSocket發(fā)送的數(shù)據(jù)報將被送到本身 socket.setLoopbackMode(false); //初始化發(fā)送用的DatagramSocket,它包含一個長度為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的線程 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類:
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(); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。