编写基于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);