基于UDP传输协议局域网文件接收软件设计 Java版
网路传输主要的两大协议为TCP/IP协议和UDP协议,本文主要介绍基于UDP传输的一个小软件分享,针对于Java网络初学者是一个很好的练笔,大家可以参考进行相关的联系,但愿能够帮助到大家。
话不多说,直接上代码结构图:
FindThread类是为了创建接收服务线程,GetLocalHostAddress类是为了获取本地IP地址,作为远程传输的目标地址,GetLocalTime是为了获取本地时间,为主界面提示信息建立时间戳,GetRandomNumber是为了方便创建端口号的,端口号正常范围是0到65535,(具体信息请百度,谢谢,有的人老是问为什么,我不禁要说一句How old are you!为什么不能自己去查一查呢!)GetSavePath类是为了实现存储功能,路径由用户自定义,MyRunable是上文所提线程的实例化,UDPReceiveSever类是详细实现协议功能,也是这个软件的核心部分。下面具体详细的列一下实现代码:
FindThread类
1 package fc; 2 3 public class FindThread { 4 public static Thread findThread(long threadId) { 5 ThreadGroup group = Thread.currentThread().getThreadGroup(); 6 while(group != null) { 7 Thread[] threads = new Thread[(int)(group.activeCount() * 1.2)]; 8 int count = group.enumerate(threads, true); 9 for(int i = 0; i < count; i++) { 10 if(threadId == threads[i].getId()) { 11 return threads[i]; 12 } 13 } 14 group = group.getParent(); 15 } 16 return null; 17 } 18 }
GetLocalHostAddress类
1 package fc; 2 3 import java.net.InetAddress; 4 import java.net.UnknownHostException; 5 6 public class GetLocalHostAddress { 7 8 public String getLocalHostAddress() throws UnknownHostException{ 9 InetAddress ia = InetAddress.getLocalHost(); 10 String ip= ia.getHostAddress(); 11 return ip; 12 } 13 14 }
GetLocalTime类
1 package fc; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 public class GetLocalTime { 7 8 public String getLocalTime(){ 9 Date date = new Date(); 10 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 11 String time = dateFormat.format(date); 12 return time; 13 } 14 }
GetRandomNumber类
1 package fc; 2 3 import java.util.Random; 4 5 public class GetRandomNumber { 6 7 public int getRandomNumber(){ 8 int max=65535; 9 int min=1025; 10 Random random_N = new Random(); 11 int num = random_N.nextInt(max)%(max-min+1) + min; 12 return num; 13 } 14 }
GetSavePath类
1 package fc; 2 3 import java.io.File; 4 5 import javax.swing.JFileChooser; 6 import javax.swing.JLabel; 7 8 public class GetSavePath { 9 10 private String filepath = ""; 11 public String getSavePath(){ 12 JFileChooser jfc=new JFileChooser(); 13 jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 14 jfc.showDialog(new JLabel(), "选择路径"); 15 File file=jfc.getSelectedFile(); 16 if(file.isDirectory()){ 17 }else if(file.isFile()){ 18 } 19 this.filepath=file.getAbsolutePath(); 20 return filepath; 21 } 22 }
MyRunable类
1 package fc; 2 3 import java.io.IOException; 4 5 public class MyRunnable implements Runnable { 6 public void run() { 7 UDPReceiveService urs = new UDPReceiveService(); 8 try { 9 urs.receiveFile(); 10 } catch (IOException | InterruptedException e) { 11 e.printStackTrace(); 12 } 13 } 14 }
UDPReceiveService类
1 package fc; 2 3 import java.io.BufferedOutputStream; 4 import java.io.DataOutputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.net.DatagramPacket; 8 import java.net.DatagramSocket; 9 10 import fm.MainWindows; 11 12 public class UDPReceiveService { 13 14 public static String IP = ""; 15 public static String filePath = ""; 16 public static int Port = 0; 17 GetLocalTime glt = new GetLocalTime(); 18 public static long thr_id; 19 20 @SuppressWarnings("static-access") 21 void receiveFile() throws IOException, InterruptedException { 22 MainWindows.jta.setText(""); 23 if(filePath.equals("")){ 24 MainWindows.jta.append("【"+glt.getLocalTime()+"】 "+"服务启动失败......错误:文件存储路径无效或为空!\n"); 25 MainWindows.jta.setCaretPosition(MainWindows.jta.getText().length()); 26 Thread.currentThread().interrupt(); 27 } 28 thr_id = Thread.currentThread().getId(); 29 @SuppressWarnings("resource") 30 DatagramSocket dataSocket = new DatagramSocket(Port); 31 MainWindows.jta.append("【"+glt.getLocalTime()+"】 "+"服务准备启动......\n"); 32 MainWindows.jta.setCaretPosition(MainWindows.jta.getText().length()); 33 byte[] receiveByte; 34 DatagramPacket dataPacket; 35 @SuppressWarnings("resource") 36 DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filePath))); 37 MainWindows.jta.append("【"+glt.getLocalTime()+"】 "+"创建接收路径已完成!\n"); 38 MainWindows.jta.setCaretPosition(MainWindows.jta.getText().length()); 39 int i = 0; 40 MainWindows.jta.append("【"+glt.getLocalTime()+"】 "+"服务已经启动!\n"); 41 MainWindows.jta.setCaretPosition(MainWindows.jta.getText().length()); 42 MainWindows.jt3_1.setText("服务正在运行......"); 43 MainWindows.jta.setCaretPosition(MainWindows.jta.getText().length()); 44 int num1 = 0,num2 = 0; 45 double num3 = 0; 46 while(i == 0){ 47 receiveByte = new byte[64512]; 48 dataPacket = new DatagramPacket(receiveByte, receiveByte.length); 49 dataSocket.receive(dataPacket); 50 i = dataPacket.getLength(); 51 if(i > 0){ 52 MainWindows.jta.append("【"+glt.getLocalTime()+"】 "+"捕获到数据包,大小为"+i/1024+"KB,数据存储已完成!\n"); 53 MainWindows.jta.setCaretPosition(MainWindows.jta.getText().length()); 54 fileOut.write(receiveByte,0,i); 55 fileOut.flush(); 56 num3 = num3 + i/1024; 57 i = 0; 58 num2 = num2 + 1; 59 } 60 if(num2 > 0 && num2 == num1){ 61 MainWindows.jta.append("【"+glt.getLocalTime()+"】 "+"文件存储已完成,大小:"+num3/1024+"M\n"); 62 MainWindows.jta.setCaretPosition(MainWindows.jta.getText().length()); 63 Thread.currentThread().sleep(10); 64 MainWindows.jta.append("【"+glt.getLocalTime()+"】 "+"服务重载成功,正在运行......\n"); 65 MainWindows.jta.setCaretPosition(MainWindows.jta.getText().length()); 66 num2 = 0; 67 } 68 num1=num2; 69 } 70 } 71 }
窗体文件就不再详细展出了,运行结果如下图: