熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

利用Java語言實現數據報編程之單播

2022-06-13   來源: Java核心技術 

  實際上單播就是一個報文從源主機向另一個目標主機發送接收目標只有一個

  首先寫收發器類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);

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25587.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.