android如何设置多条广播,Android使用MulticastSocket实现多点广播图片
DatagramSocket只允許數(shù)據(jù)報(bào)發(fā)送給指定的目標(biāo)地址,而MulticastSocket可以將數(shù)據(jù)報(bào)以廣播的方式發(fā)送至多個(gè)客戶端。其主要思想是設(shè)置一組特殊網(wǎng)絡(luò)地址作為多點(diǎn)廣播地址,每個(gè)多點(diǎn)廣播地址都被看做一個(gè)組,當(dāng)客戶端需要發(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ù)將被自動廣播到加入該地址的所有MulticastSocket,同時(shí)也可以設(shè)置該MulticastSocket接收自身發(fā)送的數(shù)據(jù)。
如果僅僅是用于發(fā)送數(shù)據(jù)報(bào)的MulticastSocket對象,使用默認(rèn)地址,隨機(jī)端口即可。但如果創(chuàng)建接收用的MulticastSocket對象,則該MulticastSocket對象必須指定端口,否則發(fā)送方無法確定發(fā)送數(shù)據(jù)報(bào)的目標(biāo)端口。
下面通過一個(gè)簡單的例子實(shí)現(xiàn)多點(diǎn)廣播圖片:
多點(diǎ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實(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對象
private DatagramPacket inPacket = new DatagramPacket(inBuff , inBuff.length);
//定義一個(gè)用于發(fā)送的DatagramPacket對象
private DatagramPacket outPacket = null;
private Handler handler;
//構(gòu)造器,初始化資源
public ComUtil(Handler handler) throws Exception
{
this.handler = handler;
//因?yàn)樵揗ultcastSocket對象需要接受數(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è)長度為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í)有所幫助,也希望大家多多支持腳本之家。
總結(jié)
以上是生活随笔為你收集整理的android如何设置多条广播,Android使用MulticastSocket实现多点广播图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 自定义 圆环,Andro
- 下一篇: android requestparam