UDP广播,实际上是很简单的,理解IP编址方案,就理解什么是广播,很多文章称为多播,我认为称为广播比较好,因为多播是对广播和组播的泛指,在Java程序中处理自然是不同的了,本文和下一篇只为强调概念.
正如前面所说,广播地址包括定向广播地址和有限广播地址,TCP/IP协议规定,这两种地址只能在本地网络中出现,路由器执行了这个规定,不把目标地址为广播地址的报文转发到其它网络,而组播就不同,下一篇介绍组播编程.
这个广播的程序可以简单的通过修改单播程序中的报文的目标地址成广播地址即可成为广播程序,缺省情况下DatagramSocket 类的广播功能是打开着的.
package recmail.multiservice;
import java.net.DatagramSocket;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
public class DatagramRecvSender_b extends DataSwapListenerAdapter
implements Runnable {
public static final int DatagramPort = 10698;
private java.net.DatagramSocket road;
private InetAddress ia;
DatagramPacket dp;
public static final int Datagram_Length = 8192;
private DataPacket cdp;
private boolean ishandling = true;
DataSwapEvent dsevent;
/**
* 这个构造器用于接收数据报.
*/
public DatagramRecvSender_b() {
try {
System.out.println("recv ...");
dsevent = new DataSwapEvent(this);
SocketAddress sa = new InetSocketAddress(
DatagramRecvSender_b.DatagramPort);
//至少要指定数据报接收端口,这是对接收端的唯一要求.当然如果主机是多址主机,需要road = new DatagramSocket(sa);
road = new DatagramSocket(DatagramRecvSender_b.DatagramPort);
System.out.println(road.isBound()+"::"+road.getBroadcast());
}
catch (SocketException ex) {
ex.printStackTrace();
}
catch (Exception ex1) {
ex1.printStackTrace();
}
}
/**
* 这个构造器用于发送数据报.
* @param send String
*/
public DatagramRecvSender_b(String send) {
try {
System.out.println("send ...");
//这个类可以计算本地网的定向广播地址.
BroadcastAddr info = new BroadcastAddr();
info.setNetmask("255.255.255.0");
info.setNetaddr("192.168.0.106");
info.execCalc();
ia = InetAddress.getByName(info.getNetbroadcastaddr());//"255.255.255.255"
dsevent = new DataSwapEvent(this);
SocketAddress sa = new InetSocketAddress(ia,
DatagramRecvSender_b.DatagramPort);
//如果需要接收数据报,就需要指定一个端口,否则不必指定.road = new DatagramSocket(DatagramRecvSender_s.DatagramPort-1);
road = new DatagramSocket();
//DatagramSocket类实例的getBroadcast()返回true,即该类的广播属性缺省设置是true,就是使能的.
//road.setBroadcast(true);可以测试一下 当执行了road.setBroadcast(false)后会出现什么状况.
System.out.println("isBound--"+road.isBound()+":getBroadcast--"+road.getBroadcast());
}
catch (SocketException ex) {
ex.printStackTrace();
}
catch (UnknownHostException ex1) {
ex1.printStackTrace();
}
}
public void sendFile(String file) {
cdp = new DataPacket(file);
System.out.println(file);
Iterator it = cdp.getDataPackets().iterator();
byte[] b;
while (it.hasNext()) {
b = ( (DataEntry) it.next()).getByte();
DatagramPacket dp = new DatagramPacket(b, 0, b.length, ia,DatagramRecvSender_b.DatagramPort);
try {
Thread.sleep(100);
if(dp==null)
continue;
road.send(dp);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
public boolean isHandling() {
return ishandling;
}
public void isHandling(boolean ishand) {
ishandling = ishand;
}
public void run() {
byte[] buffer = new byte[DataPacket.DataSwapSize];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
DataPacket dp = new DataPacket();
while (ishandling) {
packet.setLength(buffer.length);
System.out.println("wait .. ");
try {
road.receive(packet);
dp.Add(packet.getData());
if (dp.isFull()) {
dsevent.setImage(dp.Gereratedata());
this.processRecvFinishedEvent(dsevent);
dp = new DataPacket();
}
}
catch (IOException ex) {
System.out.println(ex);
}
}
}
public static void main(String[] args) {
String file[];
ArrayList al = new ArrayList();
//换成你机器上的图象文件夹
String path = "E://nature//";
File f = new File(path);
file = f.list();
DatagramRecvSender_b dgrs = new DatagramRecvSender_b("");
for(int j=0;j<1000;j++)
for (int i = 0; i < file.length; i++) {
try {
Thread.sleep(2000);
}
catch (InterruptedException ex) {
}
if (file[i].endsWith("jpg") || file[i].endsWith("bmp"))
dgrs.sendFile(path + file[i]);
}
}
}
端显示界面和单播相同,修要修改的只有一句:
在类testDual的main方法中,把DatagramRecvSender_s改为DatagramRecvSender_b即可,把接收端放到你的局域网上的几个机器上同时运行,你应该会看到不断显示的图象.
public static void main(String[] args) {
testDual test = new testDual();
test.initmain();
test.go();
DatagramRecvSender_s dgrs = new DatagramRecvSender_s();
dgrs.addDataSwapListener(test);
Thread thread = new Thread(dgrs, "test");
thread.start();
}
下一篇介绍组播编程.