Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2342197
  • 博文数量: 321
  • 博客积分: 3440
  • 博客等级: 中校
  • 技术积分: 2992
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-24 09:08
个人简介

我就在这里

文章分类

全部博文(321)

文章存档

2015年(9)

2014年(84)

2013年(101)

2012年(25)

2011年(29)

2010年(21)

2009年(6)

2008年(23)

2007年(23)

分类: Java

2014-08-29 21:04:33

常量

  1. package com.way.constants;

  2. public class Constants {
  3.     public static final String SENDIP = "localhost";// 文件发送者ip地址
  4.     public static final String RECEIVEIP = "localhost";// 文件接收者ip地址
  5.     public static final int SENDPORT = 8888;// 文件发送者监听端口
  6.     public static final int RECEIVEPORT = 9999;// 文件接收者监听端口
  7.     public static final String SENDPATH = "D:\\1.jpg";// 文件发送者文件路径
  8.     public static final String RECEIVEPATH = "D:\\2.jpg";// 文件接收者文件保存路径

  9. }

文件发送端启动

  1. package com.way.client;

  2. import com.way.constants.Constants;

  3. public class ClientTest {

  4.     public static void main(String[] args) {
  5.         //文件发送者启动并传参
  6.         new Client(Constants.RECEIVEPORT, Constants.SENDPORT,
  7.                 Constants.SENDPATH, Constants.RECEIVEIP).start();
  8.     }
  9. }

文件发送端

  1. package com.way.client;

  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.net.DatagramPacket;
  8. import java.net.DatagramSocket;
  9. import java.net.InetSocketAddress;
  10. import java.net.SocketException;

  11. public class Client extends Thread {
  12.     private DatagramSocket send;
  13.     private DatagramPacket pkg;
  14.     private DatagramPacket messagepkg;
  15.     private int serverPort;
  16.     private int clientPort;
  17.     private String path;
  18.     private File file;
  19.     private String ip;


  20.     public Client(int serverPort, int clientPort, String path, String ip) {
  21.         super();
  22.         this.serverPort = serverPort;
  23.         this.clientPort = clientPort;
  24.         this.path = path;
  25.         this.ip = ip;
  26.     }

  27.     public String getPath() {
  28.         return path;
  29.     }

  30.     public void setPath(String path) {
  31.         this.path = path;
  32.     }

  33.     public String getIp() {
  34.         return ip;
  35.     }

  36.     public void setIp(String ip) {
  37.         this.ip = ip;
  38.     }

  39.     public void send() {
  40.         try {
  41.             //文件发送者设置监听端口
  42.             send = new DatagramSocket(clientPort);
  43.             BufferedInputStream bis = new BufferedInputStream(
  44.                     new FileInputStream(new File(path)));
  45.             //确认信息包
  46.             byte[] messagebuf = new byte[1024];
  47.             messagepkg = new DatagramPacket(messagebuf, messagebuf.length);
  48.             //文件包
  49.             byte[] buf = new byte[1024 * 63];
  50.             int len;
  51.             while ((len = bis.read(buf)) != -1) {
  52.                 
  53.                 pkg = new DatagramPacket(buf, len, new InetSocketAddress(
  54.                         ip, serverPort));
  55.                 //设置确认信息接收时间,3秒后未收到对方确认信息,则重新发送一次
  56.                 send.setSoTimeout(3000);
  57.                 while (true) {
  58.                     send.send(pkg);
  59.                     send.receive(messagepkg);
  60.                     System.out.println(new String(messagepkg.getData()));
  61.                     break;
  62.                 }
  63.             }
  64.             // 文件传完后,发送一个结束包
  65.             buf = "end".getBytes();
  66.             DatagramPacket endpkg = new DatagramPacket(buf, buf.length,
  67.                     new InetSocketAddress(ip, serverPort));
  68.             System.out.println("文件发送完毕");
  69.             send.send(endpkg);
  70.             bis.close();
  71.             send.close();

  72.         } catch (SocketException e) {
  73.             e.printStackTrace();
  74.         } catch (FileNotFoundException e) {
  75.             e.printStackTrace();
  76.         } catch (IOException e) {
  77.             // TODO Auto-generated catch block
  78.             e.printStackTrace();
  79.         }
  80.     }

  81.     public void run() {
  82.         send();
  83.     }
  84. }

文件接收端启动

  1. package com.way.server;

  2. import com.way.constants.Constants;

  3. public class serverTest {
  4.     public static void main(String[] args) {
  5.         //文件接收者启动并传参
  6.         new Server(Constants.SENDIP, Constants.RECEIVEPORT, Constants.SENDPORT,
  7.                 Constants.RECEIVEPATH).start();
  8.     }
  9. }

文件接收端

  1. package com.way.server;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.net.DatagramPacket;
  10. import java.net.DatagramSocket;
  11. import java.net.InetSocketAddress;
  12. import java.net.SocketException;

  13. public class Server extends Thread {
  14.     private DatagramSocket receive;
  15.     private String ip;
  16.     private int serverPort;
  17.     private int clientPort;
  18.     private File file;
  19.     private String path;
  20.     private DatagramPacket pkg;
  21.     private DatagramPacket messagepkg;


  22.     public Server(String ip, int serverPort, int clientPort, String path) {
  23.         super();
  24.         this.ip = ip;
  25.         this.serverPort = serverPort;
  26.         this.clientPort = clientPort;
  27.         this.path = path;
  28.     }

  29.     public String getPath() {
  30.         return path;
  31.     }

  32.     public String getip() {
  33.         return ip;
  34.     }

  35.     public void setPath(String path) {
  36.         this.ip = path;
  37.     }

  38.     public File getFile() {
  39.         return file;
  40.     }

  41.     public void setFile(File file) {
  42.         this.file = file;
  43.     }

  44.     public void receive() {
  45.         try {
  46.             // 接收文件监听端口
  47.             receive = new DatagramSocket(serverPort);
  48.             // 写文件路径
  49.             BufferedOutputStream bos = new BufferedOutputStream(
  50.                     new FileOutputStream(new File(path)));

  51.             // 读取文件包
  52.             byte[] buf = new byte[1024 * 63];
  53.             pkg = new DatagramPacket(buf, buf.length);
  54.             // 发送收到文件后 确认信息包
  55.             byte[] messagebuf = new byte[1024];
  56.             messagebuf = "ok".getBytes();
  57.             messagepkg = new DatagramPacket(messagebuf, messagebuf.length,
  58.                     new InetSocketAddress(ip, clientPort));
  59.             // 循环接收包,每接到一个包后回给对方一个确认信息,对方才发下一个包(避免丢包和乱序),直到收到一个结束包后跳出循环,结束文件传输,关闭流
  60.             while (true) {
  61.                 receive.receive(pkg);
  62.                 if (new String(pkg.getData(), 0, pkg.getLength()).equals("end")) {
  63.                     System.out.println("文件接收完毕");
  64.                     bos.close();
  65.                     receive.close();
  66.                     break;
  67.                 }
  68.                 receive.send(messagepkg);
  69.                 System.out.println(new String(messagepkg.getData()));
  70.                 bos.write(pkg.getData(), 0, pkg.getLength());
  71.                 bos.flush();
  72.             }
  73.             bos.close();
  74.             receive.close();
  75.         } catch (SocketException e) {
  76.             e.printStackTrace();
  77.         } catch (FileNotFoundException e) {
  78.             e.printStackTrace();
  79.         } catch (IOException e) {
  80.             e.printStackTrace();
  81.         }
  82.     }

  83.     public void run() {
  84.         receive();
  85.     }
  86. }

转自:
阅读(724) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~