|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java到底会发战成什么样,让我们拭目以待吧,我始终坚信着java会更好。以上都是俺个人看法,欢迎大家一起交流.编程|数据实践上,单播就是一个报文从源主机向另外一个方针主机发送,吸收方针只要一个.
起首,写收发器类DatagramRecvSender_s,这个类既要发送数据报又要吸收数据报,发送一个文件夹中的一切图片,一张一张的举行,吸收是在一个线程中举行,每收完一个文件,就激活一个事务,事务处置器处置这个事务.
类清单为:
packagerecmail.multiservice;
importjava.net.DatagramPacket;
importjava.net.DatagramSocket;
importjava.net.SocketException;
importjava.net.SocketAddress;
importjava.net.UnknownHostException;
importjava.net.InetSocketAddress;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.net.InetAddress;
importjava.io.File;
importjava.util.Iterator;
publicclassDatagramRecvSender_sextendsDataSwapListenerAdapter
implementsRunnable{
publicstaticfinalintDatagramPort=10698;
privatejava.net.DatagramSocketroad;
privateInetAddressia;
DatagramPacketdp;
publicstaticfinalintDatagram_Length=8192;
privateDataPacketcdp;
privatebooleanishandling=true;
DataSwapEventdsevent;
/**
*这个机关器用于吸收数据报.
*/
publicDatagramRecvSender_s(){
try{
System.out.println("recv...");
ia=InetAddress.getByName("localhost");//192.168.0.17"127.0.0.1"
dsevent=newDataSwapEvent(this);
SocketAddresssa=newInetSocketAddress(ia,
DatagramRecvSender_s.DatagramPort);
//最少要指定命据报吸收端口,这是对吸收真个独一请求.固然假如主机是多址主机,必要road=newDatagramSocket(sa);
road=newDatagramSocket(DatagramRecvSender_s.DatagramPort);
System.out.println(road.isBound()+"::"+road.getBroadcast());
}
catch(SocketExceptionex){
ex.printStackTrace();
}
catch(Exceptionex1){
ex1.printStackTrace();
}
}
/**
*这个机关器用于发送数据报.
*@paramsendString
*/
publicDatagramRecvSender_s(Stringsend){
try{
System.out.println("startsending...");
ia=InetAddress.getByName("192.168.0.4");//"255.255.255.255""192.168.0.4""127.0.0.1"
dsevent=newDataSwapEvent(this);
SocketAddresssa=newInetSocketAddress(ia,
DatagramRecvSender_s.DatagramPort);
//假如必要吸收数据报,就必要指定一个端口,不然不用指定.road=newDatagramSocket(DatagramRecvSender_s.DatagramPort-1);
road=newDatagramSocket();
System.out.println("isBound--"+road.isBound()+":getBroadcast--"+road.getBroadcast());
}
catch(SocketExceptionex){
ex.printStackTrace();
}
catch(UnknownHostExceptionex1){
ex1.printStackTrace();
}
}
publicvoidsendFile(Stringfile){
cdp=newDataPacket(file);
System.out.println(file);
Iteratorit=cdp.getDataPackets().iterator();
byte[]b;
while(it.hasNext()){
b=((DataEntry)it.next()).getByte();
//数据报是自包括的,自形貌的数据,它的送达是由路由器实行的,路由器路由算法依据报文的方针地点选择得当的路由递送报文,
//以是数据报中要指定方针IP地点和方针主机处置端口.
DatagramPacketdp=newDatagramPacket(b,0,b.length,ia,DatagramRecvSender_s.DatagramPort);
try{
Thread.sleep(100);
if(dp==null)
continue;
road.send(dp);
}
catch(Exceptionex){
ex.printStackTrace();
}
}
}
publicbooleanisHandling(){
returnishandling;
}
publicvoidisHandling(booleanishand){
ishandling=ishand;
}
publicvoidrun(){
byte[]buffer=newbyte[DataPacket.DataSwapSize];
DatagramPacketpacket=newDatagramPacket(buffer,buffer.length);
DataPacketdp=newDataPacket();
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=newDataPacket();
}
}
catch(IOExceptionex){
System.out.println(ex);
}
}
}
publicstaticvoidmain(String[]args){
Stringfile[];
ArrayListal=newArrayList();
Stringpath="E:
ature";
Filef=newFile(path);
file=f.list();
DatagramRecvSender_sdgrs=newDatagramRecvSender_s("");
for(intj=0;j<1000;j++)
for(inti=0;i<file.length;i++){
try{
Thread.sleep(2000);
}
catch(InterruptedExceptionex){
}
if(file[i].endsWith("jpg")||file[i].endsWith("bmp"))
dgrs.sendFile(path+file[i]);
}
}
}
上面看吸收真个界面.
packagerecmail.multiservice;
importjava.awt.BorderLayout;
publicclasstestDual
extendsDualWindowimplementsDataSwapListener{
JImagePanelip;
java.awt.Imagebi;
publictestDual(){
}
publicvoidinit(){
super.init();
ImageShowis=newImageShow();
is.addDataSwapListener(this);
Threadthread=newThread(is,"test");
thread.start();
}
publicstaticvoidmain(String[]args){
testDualtest=newtestDual();
test.initmain();
test.go();
DatagramRecvSender_sdgrs=newDatagramRecvSender_s();
dgrs.addDataSwapListener(test);
Threadthread=newThread(dgrs,"test");
thread.start();
}
publicvoidinitmain(){
super.initmain();
ip=newJImagePanel();
root.setLayout(newBorderLayout(5,5));
root.add(ip,BorderLayout.CENTER);
}
publicvoidOnDataSendFinished(Objects,DataSwapEvente){
}
publicvoidOnDataRecvFinished(Objects,DataSwapEvente){
this.bi=e.getImage();
ip.setImage(bi);
System.out.println("recvFinished!");
}
}
testDual扩大的DualWindow类清单:
packagerecmail.multiservice;
importjavax.swing.JPanel;
importjavax.swing.JFrame;
importjavax.swing.JApplet;
publicclassDualWindow
extendsJApplet{
protectedJPanelroot;
privateJFrameframe;
publicDualWindow(){
}
publicvoidinit(){
root=newJPanel();
this.setContentPane(root);
}
publicvoidinitmain(){
root=newJPanel();
}
publicstaticvoidmain(String[]args){
testDualtest=newtestDual();
test.initmain();
test.start();
test.go();
}
publicvoidgo(){
frame=newJFrame();
frame.setContentPane(root);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.validate();
frame.setVisible(true);
}
}
下一篇先容播送的完成.
什么时候上述的三种开发工具能和三为一,什么时候java的竞争力才更强,才有机会拉拢更多的程序员投入到对java的开发上,因为到时的开发工具将会比.net的更简单。还有一点也很关键,什么时候java推出的jsf能成为真正意义上的标准。 |
|