[ 登录注册 ]

Java

利用Java语言实现数据报编程之单播

2016-08-25 12:20:47 admin 返回上一页

实际上,单播就是一个报文从源主机向另一个目标主机发送,接收目标只有一个。

  首先,写收发器类DatagramRecvSender_s,这个类既要发送数据报又要接收数据报,发送一个文件夹中的所有图片,一张一张的进行,接收是在一个线程中进行,每收完一个文件,就激活一个事件,事件处理器处理这个事件。

  类清单为:

package recmail.multiservice;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.util.ArrayList;
import java.net.InetAddress;
import java.io.File;
import java.util.Iterator;

public class DatagramRecvSender_s 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_s() {
  try {
   System.out.println("recv ...");
   ia = InetAddress.getByName("localhost");//192.168.0.17"127.0.0.1"
   dsevent = new DataSwapEvent(this);
   SocketAddress sa = new InetSocketAddress(ia,
   DatagramRecvSender_s.DatagramPort);
   //至少要指定数据报接收端口,这是对接收端的唯一要求.当然如果主机是多址主机,需要road = new DatagramSocket(sa);
   road = new DatagramSocket(DatagramRecvSender_s.DatagramPort);
   System.out.println(road.isBound()+"::"+road.getBroadcast());
  }
  catch (SocketException ex) {
   ex.printStackTrace();
  }
  catch (Exception ex1) {
   ex1.printStackTrace();
  }
 }
 /**
 * 这个构造器用于发送数据报.
 * @param send String
 */
 public DatagramRecvSender_s(String send) {
  try {
   System.out.println("start sending ...");
   ia = InetAddress.getByName("192.168.0.4");//"255.255.255.255""192.168.0.4""127.0.0.1"
   dsevent = new DataSwapEvent(this);
   SocketAddress sa = new InetSocketAddress(ia,
   DatagramRecvSender_s.DatagramPort);
   //如果需要接收数据报,就需要指定一个端口,否则不必指定.road = new DatagramSocket(DatagramRecvSender_s.DatagramPort-1);
   road = new DatagramSocket();
   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();
   //数据报是自包含的,自描述的数据,它的投递是由路由器执行的,路由器路由算法根据报文的目标地址选择适当的路由递送报文,
   //所以数据报中要指定目标IP地址和目标主机处理端口.
   DatagramPacket dp = new DatagramPacket(b, 0, b.length, ia,DatagramRecvSender_s.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_s dgrs = new DatagramRecvSender_s("");
  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]);
   }
  }
 }

  下面看接收端的界面.

package recmail.multiservice;

import java.awt.BorderLayout;

public class testDual
extends DualWindow implements DataSwapListener {
 JImagePanel ip;
 java.awt.Image bi;

 public testDual() {}

 public void init() {
  super.init();
  ImageShow is = new ImageShow();
  is.addDataSwapListener(this);
  Thread thread = new Thread(is, "test");
  thread.start();
 }

 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();
 }

 public void initmain() {
  super.initmain();
  ip = new JImagePanel();
  root.setLayout(new BorderLayout(5, 5));
  root.add(ip, BorderLayout.CENTER);
 }

 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!");
 }
}

  testDual扩展的DualWindow类清单:

package recmail.multiservice;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JApplet;

public class DualWindow
extends JApplet {
 protected JPanel root;
 private JFrame frame;
 public DualWindow() {}

 public void init() {
  root = new JPanel();
  this.setContentPane(root);
 }

 public void initmain() {
  root = new JPanel();
 }

 public static void main(String[] args) {
  testDual test = new testDual();
  test.initmain();
  test.start();
  test.go();
 }

 public void go() {
  frame = new JFrame();
  frame.setContentPane(root);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(300, 200);
  frame.validate();
  frame.setVisible(true);
 }
}

点击复制链接 与好友分享!回本站首页

文章来源:http://www.bozhiyue.com/java/2016/0825/430062.html
返回上一页    返回分类 上一篇:   下一篇:
相关