udp广播,实际上是很简单的,理解ip编址方案,就理解什么是广播,很多文章称为多播,我认为称为广播比较好,因为多播是对广播和组播的泛指,在java程序中处理自然是不同的了,本文和下一篇只为强调概念.
正如前面所说,广播地址包括定向广播地址和有限广播地址,tcp/ip协议规定,这两种地址只能在本地网络中出现,路由器执行了这个规定,不把目标地址为广播地址的报文转发到其它网络,而组播就不同,下一篇介绍组播编程.
这个广播的程序可以简单的通过修改单播程序中的报文的目标地址成广播地址即可成为广播程序.
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]);
}
}
}
public static void main(string[] args) {
string file[];
arraylist al = new arraylist();
string path = “e:\\nature\\”; //nature mzip
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”))
//system.out.println(file[i]);
//al.add(path + file[i]);
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();
}
下一篇介绍组播编程.