网络编程应用:基于UDP协议【实现文件下载】--练习
生活随笔
收集整理的這篇文章主要介紹了
网络编程应用:基于UDP协议【实现文件下载】--练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
要求:
基于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 blocke.printStackTrace();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(socket!=null){socket.close();}if(fos!=null){try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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中的Socketsocket=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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(socket!=null){socket.close();}if(fis!=null){try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} } }運行結果:
發送方:
接收方:
總結
以上是生活随笔為你收集整理的网络编程应用:基于UDP协议【实现文件下载】--练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络编程4之UDP协议
- 下一篇: {网络编程}和{多线程}应用:基于UDP