实际上单播就是一个报文从源主机向另一个目标主机发送接收目标只有一个
首先写收发器类DatagramRecvSender_s这个类既要发送数据报又要接收数据报发送一个文件夹中的所有图片一张一张的进行接收是在一个线程中进行每收完一个文件就激活一个事件事件处理器处理这个事件
类清单为:
package recmailmultiservice;
import DatagramPacket;
import DatagramSocket;
import SocketException;
import SocketAddress;
import UnknownHostException;
import InetSocketAddress;
import javaioIOException;
import javautilArrayList;
import InetAddress;
import javaioFile;
import javautilIterator;
public class DatagramRecvSender_s extends DataSwapListenerAdapter
implements Runnable {
public static final int DatagramPort = ;
private DatagramSocket road;
private InetAddress ia;
DatagramPacket dp;
public static final int Datagram_Length = ;
private DataPacket cdp;
private boolean ishandling = true;
DataSwapEvent dsevent;
/**
* 这个构造器用于接收数据报
*/
public DatagramRecvSender_s() {
try {
Systemoutprintln(recv );
ia = InetAddressgetByName(localhost);//
dsevent = new DataSwapEvent(this);
SocketAddress sa = new InetSocketAddress(ia
DatagramRecvSender_sDatagramPort);
//至少要指定数据报接收端口这是对接收端的唯一要求当然如果主机是多址主机需要road = new DatagramSocket(sa);
road = new DatagramSocket(DatagramRecvSender_sDatagramPort);
Systemoutprintln(roadisBound()+::+roadgetBroadcast());
}
catch (SocketException ex) {
exprintStackTrace();
}
catch (Exception ex) {
exprintStackTrace();
}
}
/**
* 这个构造器用于发送数据报
* @param send String
*/
public DatagramRecvSender_s(String send) {
try {
Systemoutprintln(start sending );
ia = InetAddressgetByName();//
dsevent = new DataSwapEvent(this);
SocketAddress sa = new InetSocketAddress(ia
DatagramRecvSender_sDatagramPort);
//如果需要接收数据报就需要指定一个端口否则不必指定road = new DatagramSocket(DatagramRecvSender_sDatagramPort);
road = new DatagramSocket();
Systemoutprintln(isBound+roadisBound()+:getBroadcast+roadgetBroadcast());
}
catch (SocketException ex) {
exprintStackTrace();
}
catch (UnknownHostException ex) {
exprintStackTrace();
}
}
public void sendFile(String file) {
cdp = new DataPacket(file);
Systemoutprintln(file);
Iterator it = cdpgetDataPackets(erator();
byte[] b;
while (ithasNext()) {
b = ( (DataEntry) itnext())getByte();
//数据报是自包含的自描述的数据它的投递是由路由器执行的路由器路由算法根据报文的目标地址选择适当的路由递送报文
//所以数据报中要指定目标IP地址和目标主机处理端口
DatagramPacket dp = new DatagramPacket(b blength iaDatagramRecvSender_sDatagramPort);
try {
Threadsleep();
if(dp==null)
continue;
roadsend(dp);
}
catch (Exception ex) {
exprintStackTrace();
}
}
}
public boolean isHandling() {
return ishandling;
}
public void isHandling(boolean ishand) {
ishandling = ishand;
}
public void run() {
byte[] buffer = new byte[DataPacketDataSwapSize];
DatagramPacket packet = new DatagramPacket(buffer bufferlength);
DataPacket dp = new DataPacket();
while (ishandling) {
packetsetLength(bufferlength);
Systemoutprintln(wait );
try {
roadreceive(packet);
dpAdd(packetgetData());
if (dpisFull()) {
dseventsetImage(dpGereratedata());
thisprocessRecvFinishedEvent(dsevent);
dp = new DataPacket();
}
}
catch (IOException ex) {
Systemoutprintln(ex);
}
}
}
public static void main(String[] args) {
String file[];
ArrayList al = new ArrayList();
String path = E:\nature\;
File f = new File(path);
file = flist();
DatagramRecvSender_s dgrs = new DatagramRecvSender_s();
for(int j=;j<;j++)
for (int i = ; i < filelength; i++) {
try {
Threadsleep();
}
catch (InterruptedException ex) {
}
if (file[i]endsWith(jpg) || file[i]endsWith(bmp))
dgrssendFile(path + file[i]);
}
}
}
下面看接收端的界面
package recmailmultiservice;
import javaawtBorderLayout;
public class testDual
extends DualWindow implements DataSwapListener {
JImagePanel ip;
javaawtImage bi;
public testDual() {}
public void init() {
superinit();
ImageShow is = new ImageShow();
isaddDataSwapListener(this);
Thread thread = new Thread(is test);
threadstart();
}
public static void main(String[] args) {
testDual test = new testDual();
testinitmain();
testgo();
DatagramRecvSender_s dgrs = new DatagramRecvSender_s();
dgrsaddDataSwapListener(test);
Thread thread = new Thread(dgrs test);
threadstart();
}
public void initmain() {
superinitmain();
ip = new JImagePanel();
rootsetLayout(new BorderLayout( ));
rootadd(ip BorderLayoutCENTER);
}
public void OnDataSendFinished(Object s DataSwapEvent e) {}
public void OnDataRecvFinished(Object s DataSwapEvent e) {
thisbi = egetImage();
ipsetImage(bi);
Systemoutprintln(recv Finished!);
}
}
testDual扩展的DualWindow类清单:
package recmailmultiservice;
import javaxswingJPanel;
import javaxswingJFrame;
import javaxswingJApplet;
public class DualWindow
extends JApplet {
protected JPanel root;
private JFrame frame;
public DualWindow() {}
public void init() {
root = new JPanel();
thissetContentPane(root);
}
public void initmain() {
root = new JPanel();
}
public static void main(String[] args) {
testDual test = new testDual();
testinitmain();
teststart();
testgo();
}
public void go() {
frame = new JFrame();
framesetContentPane(root);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetSize( );
framevalidate();
framesetVisible(true);
}
}