编写基于TCP/IP网络利用UDP通信的二进制文件传输程序,传输到指定对端后要存盘。
利用FileStreams类实现文件传输。
程序存放格式:
Receive_Thread.java
package UDP_File;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Receive_Thread extends Thread{
private static final int MAX_RECEIVE_BUFFER = 1024; //最大接收缓存
private DatagramSocket server; //DatagramSocket类用来建立服务器和客户端
private DatagramPacket packet; //DatagramPacket类用来存入和解包UDP数据
byte[] buffer = new byte[MAX_RECEIVE_BUFFER];
//构造函数
public Receive_Thread(DatagramSocket server) {
this.server = server;
packet = new DatagramPacket(buffer, buffer.length);
}
public void run() {
try {
File dest = new File("C:\\Users\\Ramer\\Desktop\\copy\\LICEcap.exe"); //要接收的文件存放路径
FileOutputStream output = new FileOutputStream(dest);
int len = 0; //数据长度
while (len == 0) { //无数据则开始循环接收数据
//接收数据包
server.receive(packet);
len = packet.getLength();
if (len > 0) {
//指定接收到数据的长度,可使程序正常接收数据
output.write(buffer,0,len);
output.flush();
len = 0;//循环接收
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Send_Thread.java
package UDP_File;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Send_Thread extends Thread {
//发送的socket端
private DatagramSocket sender = null;
//待发送的目标地址,InetSocketAddress用来储存IP套接字地址
private InetSocketAddress address = null;
//接收键盘输入
Scanner scan = new Scanner(System.in);
//构造函数
public Send_Thread(DatagramSocket sender, InetSocketAddress address) {
this.sender = sender;
this.address = address;
}
public void run() {
try {
File source = new File("C:\\Users\\Ramer\\Desktop\\LICEcap.exe"); //要传输的文件路径
InputStream inputstream = new FileInputStream(source);
byte[] data = new byte[1024];
//创建UDP数据报
while ((inputstream.read(data)) != -1) {
DatagramPacket pack = new DatagramPacket(data, data.length, address);
sender.send(pack);
TimeUnit.MICROSECONDS.sleep(1); //限制传输速度
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
Create.java
package UDP_File;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class Create { //创建服务器和客户端的类与方法
private int DEST_PORT = 0;
private int SEND_PORT = 0;
private int RECE_PORT = 0;
private String IP = null;
//构造函数储存IP和端口
public Create(int DEST_PORT, int SEND_PORT, int RECE_PORT, String IP) {
this.DEST_PORT = DEST_PORT;
this.SEND_PORT = SEND_PORT;
this.RECE_PORT = RECE_PORT;
this.IP = IP;
}
public void run(){
try {
Send_Thread send_thread = null;
Receive_Thread receive_thread = null;
InetSocketAddress address = null;
//创建待接受数据包的目的机的端口号和IP地址
address = new InetSocketAddress(IP, DEST_PORT);
//创建发送的Socket端
DatagramSocket sendsocket = new DatagramSocket(SEND_PORT);
//创建接受的Socket端
DatagramSocket recesocket = new DatagramSocket(RECE_PORT);
//发送线程建立
send_thread = new Send_Thread(sendsocket, address);
//接受线程的建立
receive_thread = new Receive_Thread(recesocket);
send_thread.start(); //开启线程
receive_thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Server.java
package UDP_File;
public class Server {
private static final int DEST_PORT = 6666;
private static final int SEND_PORT = 10001;
private static final int RECE_PORT = 8888;
private static final String IP = "127.0.0.1";
public static void main(String[] args) {
Create client = new Create(DEST_PORT,SEND_PORT,RECE_PORT,IP);
client.run();
}
}
Client.java
package UDP_File;
public class Client {
private static final int DEST_PORT = 8888;
private static final int SEND_PORT = 10000;
private static final int RECE_PORT = 6666;
private static final String IP = "127.0.0.1";
public static void main(String[] args) {
Create client = new Create(DEST_PORT,SEND_PORT,RECE_PORT,IP);
client.run();
}
}