UDP传输文件(java)

文件接收端代码   收藏代码
  1. package com.way.server;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.net.DatagramPacket;  
  11. import java.net.DatagramSocket;  
  12. import java.net.InetSocketAddress;  
  13. import java.net.SocketException;  
  14.   
  15. public class Server extends Thread {  
  16.     private DatagramSocket receive;  
  17.     private String ip;  
  18.     private int serverPort;  
  19.     private int clientPort;  
  20.     private File file;  
  21.     private String path;  
  22.     private DatagramPacket pkg;  
  23.     private DatagramPacket messagepkg;  
  24.   
  25.   
  26.     public Server(String ip, int serverPort, int clientPort, String path) {  
  27.         super();  
  28.         this.ip = ip;  
  29.         this.serverPort = serverPort;  
  30.         this.clientPort = clientPort;  
  31.         this.path = path;  
  32.     }  
  33.   
  34.     public String getPath() {  
  35.         return path;  
  36.     }  
  37.   
  38.     public String getip() {  
  39.         return ip;  
  40.     }  
  41.   
  42.     public void setPath(String path) {  
  43.         this.ip = path;  
  44.     }  
  45.   
  46.     public File getFile() {  
  47.         return file;  
  48.     }  
  49.   
  50.     public void setFile(File file) {  
  51.         this.file = file;  
  52.     }  
  53.   
  54.     public void receive() {  
  55.         try {  
  56.             // 接收文件监听端口  
  57.             receive = new DatagramSocket(serverPort);  
  58.             // 写文件路径  
  59.             BufferedOutputStream bos = new BufferedOutputStream(  
  60.                     new FileOutputStream(new File(path)));  
  61.   
  62.             // 读取文件包  
  63.             byte[] buf = new byte[1024 * 63];  
  64.             pkg = new DatagramPacket(buf, buf.length);  
  65.             // 发送收到文件后 确认信息包  
  66.             byte[] messagebuf = new byte[1024];  
  67.             messagebuf = "ok".getBytes();  
  68.             messagepkg = new DatagramPacket(messagebuf, messagebuf.length,  
  69.                     new InetSocketAddress(ip, clientPort));  
  70.             // 循环接收包,每接到一个包后回给对方一个确认信息,对方才发下一个包(避免丢包和乱序),直到收到一个结束包后跳出循环,结束文件传输,关闭流  
  71.             while (true) {  
  72.                 receive.receive(pkg);  
  73.                 if (new String(pkg.getData(), 0, pkg.getLength()).equals("end")) {  
  74.                     System.out.println("文件接收完毕");  
  75.                     bos.close();  
  76.                     receive.close();  
  77.                     break;  
  78.                 }  
  79.                 receive.send(messagepkg);  
  80.                 System.out.println(new String(messagepkg.getData()));  
  81.                 bos.write(pkg.getData(), 0, pkg.getLength());  
  82.                 bos.flush();  
  83.             }  
  84.             bos.close();  
  85.             receive.close();  
  86.         } catch (SocketException e) {  
  87.             e.printStackTrace();  
  88.         } catch (FileNotFoundException e) {  
  89.             e.printStackTrace();  
  90.         } catch (IOException e) {  
  91.             e.printStackTrace();  
  92.         }  
  93.     }  
  94.   
  95.     public void run() {  
  96.         receive();  
  97.     }  
  98. }  

 

文件传输端代码   收藏代码
  1. package com.way.client;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.IOException;  
  8. import java.net.DatagramPacket;  
  9. import java.net.DatagramSocket;  
  10. import java.net.InetSocketAddress;  
  11. import java.net.SocketException;  
  12.   
  13. public class Client extends Thread {  
  14.     private DatagramSocket send;  
  15.     private DatagramPacket pkg;  
  16.     private DatagramPacket messagepkg;  
  17.     private int serverPort;  
  18.     private int clientPort;  
  19.     private String path;  
  20.     private File file;  
  21.     private String ip;  
  22.   
  23.   
  24.     public Client(int serverPort, int clientPort, String path, String ip) {  
  25.         super();  
  26.         this.serverPort = serverPort;  
  27.         this.clientPort = clientPort;  
  28.         this.path = path;  
  29.         this.ip = ip;  
  30.     }  
  31.   
  32.     public String getPath() {  
  33.         return path;  
  34.     }  
  35.   
  36.     public void setPath(String path) {  
  37.         this.path = path;  
  38.     }  
  39.   
  40.     public String getIp() {  
  41.         return ip;  
  42.     }  
  43.   
  44.     public void setIp(String ip) {  
  45.         this.ip = ip;  
  46.     }  
  47.   
  48.     public void send() {  
  49.         try {  
  50.             //文件发送者设置监听端口  
  51.             send = new DatagramSocket(clientPort);  
  52.             BufferedInputStream bis = new BufferedInputStream(  
  53.                     new FileInputStream(new File(path)));  
  54.             //确认信息包  
  55.             byte[] messagebuf = new byte[1024];  
  56.             messagepkg = new DatagramPacket(messagebuf, messagebuf.length);  
  57.             //文件包  
  58.             byte[] buf = new byte[1024 * 63];  
  59.             int len;  
  60.             while ((len = bis.read(buf)) != -1) {  
  61.                   
  62.                 pkg = new DatagramPacket(buf, len, new InetSocketAddress(  
  63.                         ip, serverPort));  
  64.                 //设置确认信息接收时间,3秒后未收到对方确认信息,则重新发送一次  
  65.                 send.setSoTimeout(3000);  
  66.                 while (true) {  
  67.                     send.send(pkg);  
  68.                     send.receive(messagepkg);  
  69.                     System.out.println(new String(messagepkg.getData()));  
  70.                     break;  
  71.                 }  
  72.             }  
  73.             // 文件传完后,发送一个结束包  
  74.             buf = "end".getBytes();  
  75.             DatagramPacket endpkg = new DatagramPacket(buf, buf.length,  
  76.                     new InetSocketAddress(ip, serverPort));  
  77.             System.out.println("文件发送完毕");  
  78.             send.send(endpkg);  
  79.             bis.close();  
  80.             send.close();  
  81.   
  82.         } catch (SocketException e) {  
  83.             e.printStackTrace();  
  84.         } catch (FileNotFoundException e) {  
  85.             e.printStackTrace();  
  86.         } catch (IOException e) {  
  87.             // TODO Auto-generated catch block  
  88.             e.printStackTrace();  
  89.         }  
  90.     }  
  91.   
  92.     public void run() {  
  93.         send();  
  94.     }  
  95. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值