JAVA UDP通信實驗報告.doc_第1頁
JAVA UDP通信實驗報告.doc_第2頁
JAVA UDP通信實驗報告.doc_第3頁
JAVA UDP通信實驗報告.doc_第4頁
JAVA UDP通信實驗報告.doc_第5頁
已閱讀5頁,還剩5頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

2012-2013學年第一學期網絡編程技術實驗報告實驗名稱(課內課外實驗5)姓 名 _汪何媛_ 學號_100341324_ 實驗日期 2012 年 12 月10 日 實驗報告日期2012 年 12月17 日成 績 _ 一. 實驗目的1、理解并掌握數據報通信的原理2、熟練掌握利用Java語言實現C/S下的UDP通信 二實驗環境1.Myeclipse 10.0三.實驗實際完成內容及結果分析1.請請編輯并調試下面的程序,給出程序的運行過程、結果和各個類文件的功能說明。1.CudpSocket類package udp;import java.io.IOException; import .DatagramPacket; import .DatagramSocket; import .SocketException; public class CudpSocket DatagramPacket dp = null; /建一個新數據報包DatagramSocket dgsocket=null; /建一個數據報包的套接字public CudpSocket() try byte buf = new byte1000; / 構造一個新分配的 Byte 對象,表示指定的 byte 值dgsocket = new DatagramSocket(12345);/創建數據報套接字并將其綁定到本地主機上的指定端口12345dp= new DatagramPacket(buf,buf.length);/構造 DatagramPacket,用來接收長度為 length 的數據包 catch (SocketException e) e.printStackTrace(); public static void main(String args) System.out.println(enter the server);CudpSocket css = new CudpSocket();try css.dgsocket.receive(css.dp); / 從css.dp套接字接收數據報包byte data = css.dp.getData(); /為css.dp包設置數據緩沖區System.out.println(datac.length : +data.length); for(int i=0;idata.length;i+) System.out.println(datai);NetFileW nfw = new NetFileW(D:/JAVA/MyEclipse6.0/happy.txt); nfw.write(css.dp.getData();catch (IOException e) e.printStackTrace();2.NetFileR類package udp;import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class NetFileR private String filePath; public NetFileR(String filePath) this.filePath = filePath;public byte getData() throws IOException /通過將給定路徑名字符串轉換成抽象路徑名來創建一個新 File 實例File file = new File(filePath);FileInputStream filein = new FileInputStream(file); /創建一個FileInputStream流DataInputStream in = new DataInputStream(filein); /創建一個 DataInputStreambyte data = new byte1024; /構造一個新分配的 Byte 對象,表示指定的 byte 值in.read(data); return data;public String getFilePath() return filePath; public void setFilePath(String filePath) this.filePath = filePath; 3. SudpSocket類package udp;import java.io.IOException;import .DatagramPacket; import .DatagramSocket; import . Inet4Address; import .SocketException; import .UnknownHostException; public class SudpSocket private DatagramSocket dgs = null; /建一個數據報包的套接字private DatagramPacket dgp = null; /建一個新數據報包public SudpSocket(String host,int prot,byte data) try dgs = new DatagramSocket(9999);/創建數據報套接字并將其綁定到本地主機上的指定端口9999Inet4Address target=null;/建立一個IPv4地址try target = (Inet4Address) Inet4Address.getByName(host); /在給定主機名的情況下確定主機的 IP 地址 catch (UnknownHostException e) / TODO Auto-generated catch blocke .printStackTrace();dgp = new DatagramPacket(data,data.length,target,prot); /構造數據報包,用來將長度為 length 的包發送到指定主機上的指定端口號 catch (SocketException e) e.printStackTrace();public static void main(String args) NetFileR nf = new NetFileR(D:/JAVA/MyEclipse6.0/UDP/happy.txt);SudpSocket sps;try byte data = nf.getData();System.out.println(data.length : +data.length); for(int i=0;idata.length;i+) System.out.println(datai);sps = new SudpSocket(localhost,12345,data); catch (IOException e1) e1.printStackTrace(); sps=null;try sps.dgs.send(sps.dgp); catch (IOException e) e.printStackTrace(); sps=null;System.out.println(over the sending); 4. NetFileW類package udp;import java.io.File;import java.io.FileOutputStream; import java.io.IOException; public class NetFileW public NetFileW(String filePath) this.filePath = filePath;private String filePath;public void write(byte data) throws IOException /通過將給定路徑名字符串轉換成抽象路徑名來創建一個新 File 實例File file = new File(filePath);FileOutputStream out = new FileOutputStream(file);/創建一個FileOutputStream流out.write(data);public String getFilePath() return filePath;public void setFilePath(String filePath) this.filePath = filePath; 執行結果:首先,本代碼中要發送的文件目錄及文件名為D:/JAVA/ MyEclipse6.0/ UDP/happy.txt。創建好相應文件之后,先運行CudpSocket類,然后再運行SudpSocket發送文件happy.txt。結果如下:SudpSocket下的控制臺:data.length : 1024104101108108111330()0over the sendingCudpSocket下的控制臺enter the serverdata.length : 1000104101108108111330()02.請參考步驟 1 的代碼及本章的 DatagramTester.java、 MulticastSender.java、 MulticastReceiver.java程序,實現基于UDP的組播文件傳輸功能,即可以向組內用戶群發文件的功能(要求:文件大小大于1K的,設計使用多個UDP 報文進行發送)。1.MulticastSender類import .*;import java.io.*;public class MulticastSender public static void main(String args) throws Exception InetAddress group=InetAddress.getByName();/緩存 int port=4000; /設置端口4000 MulticastSocket ms = null; try ms = new MulticastSocket(port);/創建多播套接字并將其綁定到特定端口 / ms.joinGroup(group); while (true) String message = Hello + new java.util.Date(); byte buffer=message.getBytes(); DatagramPacket dp = new DatagramPacket(buffer, buffer.length,group,port); /構造數據報包,用來將長度為 length 的包發送到指定主機上的指定端口號 ms.send(dp); System.out.println(發送數據報給 +group+:+port); Thread.sleep(1000); catch (IOException e) e.printStackTrace(); finally if (ms != null) try ms.leaveGroup(group); ms.close(); catch (IOException e) 2.MulticastReceiver類import .*;import java.io.*;public class MulticastReceiver public static void main(String args) throws Exception InetAddress group=InetAddress.getByName();/設置緩存 int port=4000; MulticastSocket ms = null; try ms = new MulticastSocket(port);/創建多播套接字并將其綁定到特定端口 ms.joinGroup(group); byte buffer = new byte8192;/構造一個新分配的 Byte 對象,以表示指定的 byte 值 while (true) DatagramPacket dp = new DatagramPacket(buffer, buffer.length); /構造數據報包,用來將長度為 length 的包發送到指定主機上的指定端口號 ms.receive(dp); String s = new String(dp.getData(),0,dp.getLength(); /構造一個新的 String,方法是使用指定的字符集解碼字節的指定子數組 System.out.println(s); catch (IOException e) e.printStackTrace(); finally if (ms != null) try ms.leaveGroup(group); ms.close(); catch (IOException e) MulticastSender發送的數據包給:4000MulticastReceiver1和MulticastReceiver2分別收到兩條信息四思考題試請給出UDP支持下的Client /Server通信的全過程。可以用于管道程序通過UDP在不同機器之間的傳送1.Client類import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import .DatagramPacket;import .DatagramSocket;import .InetAddress;import .SocketException;public class CollectPipe private static final int DEFAULT_PORT = 8888; / should hold the max size of a udp packet private static final int BUFFER_LENGTH = 2048; public static void main(String args) throws Exception if (args.length 1) port = Integer.parseInt(args1); DatagramSocket aSocket = null; try aSocket = new DatagramSocket(); InetAddress serverAddress = InetAddress.getByName(args0); BufferedReader in = new BufferedReader(new InputStreamReader( System.in); String str = ; while (str != null) str = in.readLine(); if (str = null) break; byte buffer = str.getBytes(); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, serverAddress, port); aSocket.send(packet); catch (SocketException e) System.out.println(Socket: + e.getMessage(); catch (IOException e) System.out.println(IO: + e.getMessage(); finally if (aSocket != null) aSocket.close(); 2.Server 端import java.io.IOException;import .DatagramPacket;import .DatagramSocket;import .SocketException;public class CollectPipeServer private static final int DEFAULT_PORT = 8888; private static final int BUFFER_LENGTH = 2048; publi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論