网络编程应用:基于UDP协议【实现文件下载】--练习

xiaoxiao2021-02-27  286

要求:

基于UDP协议实现文件下载

发送方–请求–接收方发送文件–发送方接收文件


代码:


发送方:

package Homework1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; public class Send { public static void main(String[] args){ System.out.println("发送者启动"); DatagramSocket socket=null; FileOutputStream fos=null; try { //1.发送请求 socket=new DatagramSocket(); String string="请求下载文件..."; DatagramPacket packet=new DatagramPacket(string.getBytes(), string.getBytes().length, InetAddress.getLocalHost(), 6635); socket.send(packet); //2.创建file对象,以及文件输出流 File file=new File("b.txt"); fos=new FileOutputStream(file); //3.接收文件 byte[] buf=new byte[1024]; while(true){ DatagramPacket p=new DatagramPacket(buf, buf.length); socket.receive(p); String string2=new String(p.getData(), 0, p.getLength()); //4.根据接收方发来的数据,判断文件是否传输完毕 if(string2.equals("发送完毕")){ break; } fos.write(string2.getBytes()); fos.flush(); } } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(socket!=null){ socket.close(); } if(fos!=null){ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }

接收方:

package Homework1; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class Receive { public static void main(String[] args){ System.out.println("接收者启动"); DatagramSocket socket=null; FileInputStream fis=null; try { //1.创建Datagramsocket对象,相当于TCP中的Socket socket=new DatagramSocket(6635); //2.创建Datagrampacket对象,用于装数据 byte[] buf=new byte[1024]; DatagramPacket packet=new DatagramPacket(buf, buf.length); //3.接收数据,收到发送方请求“下载文件” socket.receive(packet); System.out.println(new String(packet.getData(),0,packet.getLength())); //4.发送文件给--发送方 File file=new File("a.txt");//要给发送方的文件 fis=new FileInputStream(file); byte[] b=new byte[10]; int count=0; while((count=fis.read(b))!=-1){ DatagramPacket p=new DatagramPacket(b, count,packet.getAddress(),packet.getPort()); socket.send(p); } //5.文件发送完后,给--发送方回复一句话 String string="发送完毕"; packet=new DatagramPacket(string.getBytes(), string.getBytes().length,packet.getAddress(),packet.getPort()); socket.send(packet); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(socket!=null){ socket.close(); } if(fis!=null){ try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }

运行结果: 发送方:

接收方:

转载请注明原文地址: https://www.6miu.com/read-4209.html

最新回复(0)