在信息时代,网络技术应用已经很普通。其中很多应用都依赖于从一个主机向多个主机或者从多个主机向多个主机发送同一信息的能力,在internet上分发的数目可能达数十万台,这些都需要更高的带宽,并且大大超出了单播的能力。一种能最大限度地利用现有带宽的重要技术是ip组播。
1.ip组播技术的概念
ip组播技术,是一种允许一台或多台主机(组播源)发送单一数据包到多台主机(一次的,同时的)的tcp/ip网络技术,是一点对多点的通信。在网络多媒体广播的应用中,当需要将一个节点的信号传送到多个节点时,无论是采用重复点对点通信方式,还是采用广播方式,都会严重浪费网络带宽,只有组播才是最好的选择。组播能使一个或多个组播源只把数据包发送给特定的组播组,而只有加入该组播组的主机才能接收到数据包。
2.ip组播地址
ip组播通信依赖于ip组播地址,在ipv4中它是一个d类ip地址,范围从224.0.0.0到239.255.255.255,并被划分为局部链接组播地址、预留组播地址和管理权限组播地址三类。其中,局部链接组播地址范围在224.0.0.0~224.0.0.255,这是为路由协议和其它用途保留的地址,路由器并不转发属于此范围的ip包;预留组播地址为224.0.1.0~238.255.255.255,可用于全球范围(如internet)或网络协议;管理权限组播地址为239.0.0.0~239.255.255.255,可供组织内部使用,类似于私有ip地址,不能用于internet,可限制组播范围。
3.组播组
使用同一个ip组播地址接收组播数据包的所有主机构成了一个主机组,也称为组播组。一个组播组的成员是随时变动的,一台主机可以随时加入或离开组播组,组播组成员的数目和所在的地理位置也不受限制,一台主机也可以属于几个组播组。此外,不属于某一个组播组的主机也可以向该组播组发送数据包。
本文使用multicastsocket类的实例编写组播应用,multicastsocket类提供连接和离开组播等操作。
multisender类清单
package recmail.multiservice;
import java.net.*;
import java.io.ioexception;
/**
* 该类封装了multicastsocket类,完成了multicastsocket类实例的创建、初始化功能,
* 并提供了一个发送数据的接口.
*/
public class multisender {
public static final int multisender_port=4099;
private multicastsocket road;
private inetaddress ia;
public multisender() {
try {
//组播地址
ia = inetaddress.getbyname(“239.66.69.18”);
road = new multicastsocket(multisender_port);
road.joingroup(ia);
}
catch (unknownhostexception ex) {
}
catch (ioexception ex1) {
}
}
public inetaddress getinetaddress(){
return ia;
}
public multicastsocket getroad(){
return road;
}
public void send(byte[] b){
datagrampacket dp = new datagrampacket(b, 0, b.length,
ia, multisender.multisender_port);
try {
road.send(dp);
}
catch (ioexception ex) {
ex.printstacktrace();
}
}
}
imageserver类,使用上面的类发送文件数据.
package recmail.multiservice;
import java.io.*;
import javax.swing.timer;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.filefilter;
import java.io.filenamefilter;
/**
* 本类利用multisender类发送文件数据到一个组播组发送数据.
*/
public class imageserver
extends thread implements actionlistener {
timer timer;
bufferedimage image;
arraylist streamfragments;
int counter = 0;
byte[] imagebyte;
arraylist listener;
multisender sender;
public imageserver(arraylist f) {
timer = new timer(50, this);
timer.addactionlistener(this);
listener = new arraylist();
streamfragments = f;
sender = new multisender();
timer.start();
}
public void adddataswaplistener(dataswaplistener dsl) {
listener.add(dsl);
}
public void removedataswaplistener(dataswaplistener dsl) {
listener.remove(dsl);
}
private void processevent() {
for (int i = 0; i < this.listener.size(); i++) {
dataswapevent dse = new dataswapevent();
( (dataswaplistener)this.listener.get(i)).ondatasendfinished(this, dse);
}
}
public void actionperformed(actionevent e) {
datapacket dp = new datapacket(streamfragments.get(counter).tostring());
dataentry de;
try {
arraylist al = dp.getdatapackets();
thread.sleep(1000);
system.out.println(streamfragments.get(counter).tostring());
for (int i = 0; i < al.size(); i++) {
imagebyte = ( (dataentry) al.get(i)).getbyte(); //(byte[]) al.get(i);
sender.send(imagebyte);
}
this.processevent();
}
catch (exception ex) {
system.out.println(ex);
}
counter++;
if (counter >= streamfragments.size())
counter = 0;
}
public void run() {
while (true) {
try {
this.sleep(20);
}
catch (interruptedexception ex) {
}
}
}
public static void main(string[] args) {
string file[];
arraylist al = new arraylist();
string path = “e:\\mzip\\”;
file f = new file(path);
file = f.list();
for (int i = 0; i < file.length; i++) {
if (file[i].endswith(“jpg”) || file[i].endswith(“bmp”))
al.add(path + file[i]);
}
imageserver is = new imageserver(al);
is.start();
}
}
package recmail.multiservice;
import java.net.*;
import java.io.*;
import java.awt.image.*;
/**
* 该类封装了multicastsocket类,完成了multicastsocket类实例的创建、初始化功能,
* 并提供一个接收数据的线程,在判断接收完毕后产生事件,更新ui显示.
* 该类由testframe使用.
*/
public class imageshow
extends dataswaplisteneradapter
implements runnable {
private inetaddress ia;
private int port = 4099;
private multicastsocket road;
dataswapevent dsevent;
java.awt.image.bufferedimage bi;
public imageshow() {
dsevent = new dataswapevent(this);
try {
ia = inetaddress.getbyname(“239.66.69.18”);
road = new multicastsocket(port);
road.joingroup(ia);
}
catch (ioexception ex) {
}
}
public void run() {
byte[] buffer = new byte[datapacket.dataswapsize];
datagrampacket packet = new datagrampacket(buffer, buffer.length);
datapacket dp = new datapacket();
while (true) {
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);
}
}
}
}
接收端界面类:
package recmail.multiservice;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
/**
* 该类使用imageshow更新显示的图象.
*/
public class testframe
extends japplet
implements dataswaplistener {
private jpanel root;
jlabel label;
jimagepanel ip;
java.awt.image bi;
public testframe() {
initmain();
}
public void init() {
initmain();
this.setcontentpane(root);
imageshow is = new imageshow();
is.adddataswaplistener(this);
thread thread = new thread(is, “test”);
thread.start();
}
public static void main(string[] args) {
testframe test = new testframe();
test.go(new jframe());
imageshow is = new imageshow();
is.adddataswaplistener(test);
thread thread = new thread(is, “test”);
thread.start();
}
public void go(jframe frame) {
frame.setcontentpane(root);
frame.setsize(300, 200);
frame.setdefaultcloseoperation(jframe.exit_on_close);
frame.validate();
frame.setvisible(true);
}
public void initmain() {
root = new jpanel();
//label = new jlabel();
ip = new jimagepanel();
root.setlayout(new borderlayout(5, 5));
root.add(ip, borderlayout.center);
}
public void setrefreshimage(java.awt.image.bufferedimage img) {
this.bi = img;
ip.setimage(bi);
}
public void paint(graphics g) {
super.paint(g);
}
public void paintcomponents(graphics g) {
super.paintcomponents(g);
graphics g1 = root.getgraphics();
g1.drawimage(bi, 0, 0, this);
}
public void ondatasendfinished(object s, dataswapevent e) {
}
public void ondatarecvfinished(object s, dataswapevent e) {
this.bi = e.getimage();
ip.setimage(bi);
system.out.println(“recv finished!”);
}
}
到此,这个多播程序编写完毕,通过这个程序可以看出在java中进行组播编程有两个特点,一是使用java的multicastsocket类,二是使用组播地址配置multicastsocket实例.
数据报编程的全部内容已结束,如果要改进,可以在两个方面进行,一个是改善传输的可靠性方面,一个是采用数据收发的异步方面,因为在j2sdk1.4中multicastsocket类增加了方法getchannel().