使用UPD 和 TCP 发送接收大文件

时间:2022-12-15 16:29:12

UDP

发送端

package com.lzj.udp;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

//发送端UPD,发送给本机得9988端口
public class SendUdp {
	public static void main(String[] args) throws Exception {
		DatagramSocket socket = new DatagramSocket(9989);

		// 发送文件缓存数组
		byte[] sendFileBuff = new byte[1024 * 40];
		BufferedInputStream bufferedIn = new BufferedInputStream(new FileInputStream(new File("E://APAA280.avi")));

		// 确认信息缓存数组
		byte[] confirmMsg = new byte[1024];
		DatagramPacket msgPacket = new DatagramPacket(confirmMsg, confirmMsg.length);

		int len = 0;
		// 循环读取需要发送的文件
		while ((len = bufferedIn.read(sendFileBuff)) != -1) {
			// 发送文件数据报 分块发送
			DatagramPacket dgp = new DatagramPacket(sendFileBuff, sendFileBuff.length, InetAddress.getLocalHost(),
					9988);
			// 等待对方回复,收到回复再继续发送,没收到回复重新发送一次
			socket.setSoTimeout(3000);
			while (true) {
				socket.send(dgp);
				socket.receive(msgPacket);//receive是阻塞方法,未收到会一直等待
				String confirmMsgString = new String(msgPacket.getData(),0,msgPacket.getLength());
				System.out.println(confirmMsgString);
				break;
			}
		}
		//文件传输完毕后发送ok
		DatagramPacket dgp = new DatagramPacket("ok".getBytes(), "ok".getBytes().length, InetAddress.getLocalHost(),9988);
		socket.send(dgp);
		socket.close();
	}

}

接收端

package com.lzj.udp;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UPDRecevie {
	public static void main(String[] args) throws Exception {
		DatagramSocket socket = new DatagramSocket(9988);
		
		//接受文件缓存数组
		byte[] recFileBuff = new byte[1024*40];
		DatagramPacket recFilePacket = new DatagramPacket(recFileBuff, 1024*40);
		//回复对方 已收到文件
		DatagramPacket comfirmMsgPacket = new DatagramPacket("getFile".getBytes(), "getFile".getBytes().length,InetAddress.getLocalHost(),9989);
		File file = new File("E://day32//hehe.avi");
		BufferedOutputStream buffOut = new BufferedOutputStream(new FileOutputStream(file));
		
		//循环接受文件,当收到对方发来的ok时,结束文件接收
		while(true){
			socket.receive(recFilePacket);
			if(new String(recFilePacket.getData(),0,recFilePacket.getLength()).equals("ok")){
				
				break;//文件接受完毕,跳出
			}
			//往缓冲流中写入收到的数据包
			buffOut.write(recFilePacket.getData(),0,recFilePacket.getLength());
			buffOut.flush();
			//给对方回复 让他继续发送
			socket.send(comfirmMsgPacket);
			
		}
		
		
	}
}

TCP

服务端:

package com.lzj.tcp;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerTCP {
	
	public static void main(String[] args) throws Exception {
		
		//创建服务端Socket
		ServerSocket serverSocket = new ServerSocket(12999);
		
		//等待客户端连接 accept阻塞方法
		Socket clientSocket = serverSocket.accept();
		
		System.out.println(clientSocket.getInetAddress()+"已连接");
		
		//文件写入缓冲流
		@SuppressWarnings("resource")
		BufferedOutputStream bufStream = new BufferedOutputStream(new FileOutputStream(new File("D://struts-2.3.24-all.zip")));
		int len = 0;int count=0;
		byte[] buff = new byte[1024*50];
		InputStream in = clientSocket.getInputStream();
		while((len = in.read(buff)) != -1){
			bufStream.write(buff, 0, len);
			System.out.println(++count);
		}
		
		//告诉客户端已经收到!
		clientSocket.getOutputStream().write("OK".getBytes());
		
	}

}

客户端

package com.lzj.tcp;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.Socket;

public class ClientTCP {
	
	public static void main(String[] args) throws Exception {
		
		//创建客户端TCP Socket
		Socket clientSocket = new Socket(InetAddress.getLocalHost(),12999);
		
		//读取需要上传的文件到缓冲流
		BufferedInputStream bufStream = new BufferedInputStream(new FileInputStream(new File("E://struts-2.3.24-all.zip")));
		int len = 0;
		byte[] buff = new byte[1024*50];
		
		//接受服务端确认的字节数组
		byte[] b = new byte[100];
		//读物缓冲流到Socket输入流
		while((len = bufStream.read(buff)) != -1){
			clientSocket.getOutputStream().write(buff, 0, len);
		}
		//发送完毕关闭输出流 输出流一旦关闭 服务端退出while循环 ,给客户端返回成功消息
		clientSocket.shutdownOutput();
	
		len = clientSocket.getInputStream().read(b);		
		if(new String(b,0,len).equals("OK")){
			System.out.println("OK");
		}
		
		
		//关闭资源
		bufStream.close();
		clientSocket.close();
		
	}

}