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

提交评论