JAVA UDP通信实验报告.docx

上传人:b****1 文档编号:652803 上传时间:2023-04-29 格式:DOCX 页数:15 大小:54.73KB
下载 相关 举报
JAVA UDP通信实验报告.docx_第1页
第1页 / 共15页
JAVA UDP通信实验报告.docx_第2页
第2页 / 共15页
JAVA UDP通信实验报告.docx_第3页
第3页 / 共15页
JAVA UDP通信实验报告.docx_第4页
第4页 / 共15页
JAVA UDP通信实验报告.docx_第5页
第5页 / 共15页
JAVA UDP通信实验报告.docx_第6页
第6页 / 共15页
JAVA UDP通信实验报告.docx_第7页
第7页 / 共15页
JAVA UDP通信实验报告.docx_第8页
第8页 / 共15页
JAVA UDP通信实验报告.docx_第9页
第9页 / 共15页
JAVA UDP通信实验报告.docx_第10页
第10页 / 共15页
JAVA UDP通信实验报告.docx_第11页
第11页 / 共15页
JAVA UDP通信实验报告.docx_第12页
第12页 / 共15页
JAVA UDP通信实验报告.docx_第13页
第13页 / 共15页
JAVA UDP通信实验报告.docx_第14页
第14页 / 共15页
JAVA UDP通信实验报告.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

JAVA UDP通信实验报告.docx

《JAVA UDP通信实验报告.docx》由会员分享,可在线阅读,更多相关《JAVA UDP通信实验报告.docx(15页珍藏版)》请在冰点文库上搜索。

JAVA UDP通信实验报告.docx

JAVAUDP通信实验报告

实验名称(课内课外实验5)

姓名_汪何媛___学号__100341324____

实验日期2012年12月10日实验报告日期2012年12月17日

成绩___________________

一.实验目的

1、理解并掌握数据报通信的原理

2、熟练掌握利用Java语言实现C/S下的UDP通信

二.实验环境

1.Myeclipse10.0

三.实验实际完成内容及结果分析

1.请请编辑并调试下面的程序,给出程序的运行过程、结果和各个类文件的功能说明。

1.CudpSocket类

packageudp;

importjava.io.IOException;

import.DatagramPacket;

import.DatagramSocket;

import.SocketException;

publicclassCudpSocket{

DatagramPacketdp=null;//建一个新数据报包

DatagramSocketdgsocket=null;//建一个数据报包的套接字

publicCudpSocket(){

try{

byte[]buf=newbyte[1000];//构造一个新分配的Byte对象,表示指定的byte值

dgsocket=newDatagramSocket(12345);//创建数据报套接字并将其绑定到本地主机上的指定端口12345

dp=newDatagramPacket(buf,buf.length);//构造DatagramPacket,用来接收长度为length的数据包

}catch(SocketExceptione){

e.printStackTrace();

}}

publicstaticvoidmain(String[]args){

System.out.println("entertheserver");

CudpSocketcss=newCudpSocket();

try{

css.dgsocket.receive(css.dp);//从css.dp套接字接收数据报包

bytedata[]=css.dp.getData();//为css.dp包设置数据缓冲区

System.out.println("datac.length:

"+data.length);

for(inti=0;i

System.out.println(data[i]);

}

NetFileWnfw=newNetFileW("D:

/JAVA/MyEclipse6.0/happy.txt");

nfw.write(css.dp.getData());

}catch(IOExceptione)

{e.printStackTrace();

}}

}

2.NetFileR类

packageudp;

importjava.io.DataInputStream;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.IOException;

publicclassNetFileR{

privateStringfilePath;

publicNetFileR(StringfilePath)

{this.filePath=filePath;

}

publicbyte[]getData()throwsIOException{

//通过将给定路径名字符串转换成抽象路径名来创建一个新File实例

Filefile=newFile(filePath);

FileInputStreamfilein=newFileInputStream(file);//创建一个FileInputStream流

DataInputStreamin=newDataInputStream(filein);//创建一个DataInputStream

bytedata[]=newbyte[1024];//构造一个新分配的Byte对象,表示指定的byte值

in.read(data);

returndata;

}

publicStringgetFilePath(){returnfilePath;}

publicvoidsetFilePath(StringfilePath)

{this.filePath=filePath;

}

}

3.SudpSocket类

packageudp;

importjava.io.IOException;

import.DatagramPacket;

import.DatagramSocket;

import.Inet4Address;

import.SocketException;

import.UnknownHostException;

publicclassSudpSocket{

privateDatagramSocketdgs=null;//建一个数据报包的套接字

privateDatagramPacketdgp=null;//建一个新数据报包

publicSudpSocket(Stringhost,intprot,byte[]data){

try{

dgs=newDatagramSocket(9999);//创建数据报套接字并将其绑定到本地主机上的指定端口9999

Inet4Addresstarget=null;//建立一个IPv4地址

try{

target=(Inet4Address)Inet4Address.getByName(host);//在给定主机名的情况下确定主机的IP地址

}

catch(UnknownHostExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

dgp=newDatagramPacket(data,data.length,target,prot);

//构造数据报包,用来将长度为length的包发送到指定主机上的指定端口号

}catch(SocketExceptione){

e.printStackTrace();}}

publicstaticvoidmain(Stringargs[]){

NetFileRnf=newNetFileR("D:

/JAVA/MyEclipse6.0/UDP/happy.txt");

SudpSocketsps;

try{

byte[]data=nf.getData();

System.out.println("data.length:

"+data.length);

for(inti=0;i

System.out.println(data[i]);}

sps=newSudpSocket("localhost",12345,data);

}catch(IOExceptione1)

{e1.printStackTrace();sps=null;}

try{

sps.dgs.send(sps.dgp);

}catch(IOExceptione)

{e.printStackTrace();sps=null;}

System.out.println("overthesending");}}

4.NetFileW类

packageudp;

importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.IOException;

publicclassNetFileW{

publicNetFileW(StringfilePath)

{this.filePath=filePath;

}

privateStringfilePath;

publicvoidwrite(byte[]data)throwsIOException{

//通过将给定路径名字符串转换成抽象路径名来创建一个新File实例

Filefile=newFile(filePath);

FileOutputStreamout=newFileOutputStream(file);//创建一个FileOutputStream流

out.write(data);

}

publicStringgetFilePath(){

returnfilePath;

}

publicvoidsetFilePath(StringfilePath)

{this.filePath=filePath;

}}

执行结果:

首先,本代码中要发送的文件目录及文件名为D:

/JAVA/MyEclipse6.0/UDP/happy.txt。

创建好相应文件之后,先运行CudpSocket类,然后再运行SudpSocket发送文件happy.txt。

结果如下:

SudpSocket下的控制台:

data.length:

1024

104

101

108

108

111

33

0

(…)

0

overthesending

 

CudpSocket下的控制台

entertheserver

data.length:

1000

104

101

108

108

111

33

0

(…)

0

 

2.请参考步骤1的代码及本章的DatagramTester.java、MulticastSender.java、MulticastReceiver.java程序,实现基于UDP的组播文件传输功能,即可以向组内用户群发文件的功能(要求:

文件大小大于1K的,设计使用多个UDP报文进行发送)。

1.MulticastSender类

import.*;

importjava.io.*;

publicclassMulticastSender{

publicstaticvoidmain(String[]args)throwsException{

InetAddressgroup=InetAddress.getByName("229.0.0.1");//缓存

intport=4000;//设置端口4000

MulticastSocketms=null;

try{

ms=newMulticastSocket(port);//创建多播套接字并将其绑定到特定端口

//ms.joinGroup(group);

while(true){

Stringmessage="Hello"+newjava.util.Date();

byte[]buffer=message.getBytes();

DatagramPacketdp=newDatagramPacket(buffer,buffer.length,group,port);

//构造数据报包,用来将长度为length的包发送到指定主机上的指定端口号

ms.send(dp);

System.out.println("发送数据报给"+group+":

"+port);

Thread.sleep(1000);

}

}catch(IOExceptione){

e.printStackTrace();

}finally{

if(ms!

=null){

try{

ms.leaveGroup(group);

ms.close();

}

catch(IOExceptione){}

}

}

}

}

2.MulticastReceiver类

import.*;

importjava.io.*;

publicclassMulticastReceiver{

publicstaticvoidmain(String[]args)throwsException{

InetAddressgroup=InetAddress.getByName("229.0.0.1");//设置缓存

intport=4000;

MulticastSocketms=null;

try{

ms=newMulticastSocket(port);//创建多播套接字并将其绑定到特定端口

ms.joinGroup(group);

byte[]buffer=newbyte[8192];//构造一个新分配的Byte对象,以表示指定的byte值

while(true){

DatagramPacketdp=newDatagramPacket(buffer,buffer.length);

//构造数据报包,用来将长度为length的包发送到指定主机上的指定端口号

ms.receive(dp);

Strings=newString(dp.getData(),0,dp.getLength());

//构造一个新的String,方法是使用指定的字符集解码字节的指定子数组

System.out.println(s);

}

}catch(IOExceptione){

e.printStackTrace();

}finally{

if(ms!

=null){

try{

ms.leaveGroup(group);

ms.close();

}

catch(IOExceptione){}

}

}

}

}

MulticastSender发送的数据包给224.0.0.1:

4000

MulticastReceiver1和MulticastReceiver2分别收到两条信息

四思考题

试请给出UDP支持下的Client/Server通信的全过程。

可以用于管道程序通过UDP在不同机器之间的传送

1.Client类

importjava.io.BufferedReader;

importjava.io.IOException;

importjava.io.InputStreamReader;

import.DatagramPacket;

import.DatagramSocket;

import.InetAddress;

import.SocketException;

publicclassCollectPipe{

privatestaticfinalintDEFAULT_PORT=8888;

//shouldholdthemaxsizeofaudppacket

privatestaticfinalintBUFFER_LENGTH=2048;

publicstaticvoidmain(String[]args)throwsException{

if(args.length<1){

System.out.printf("Usage:

java%shost[port]\n",CollectPipe.class

.getCanonicalName());

System.exit(0);

}

intport=DEFAULT_PORT;

if(args.length>1)

port=Integer.parseInt(args[1]);

DatagramSocketaSocket=null;

try{

aSocket=newDatagramSocket();

InetAddressserverAddress=InetAddress.getByName(args[0]);

BufferedReaderin=newBufferedReader(newInputStreamReader(

System.in));

Stringstr="";

while(str!

=null){

str=in.readLine();

if(str==null)

break;

byte[]buffer=str.getBytes();

DatagramPacketpacket=newDatagramPacket(buffer,

buffer.length,serverAddress,port);

aSocket.send(packet);

}

}catch(SocketExceptione){

System.out.println("Socket:

"+e.getMessage());

}catch(IOExceptione){

System.out.println("IO:

"+e.getMessage());

}finally{

if(aSocket!

=null)

aSocket.close();

}

}

}

2.Server端

importjava.io.IOException;

import.DatagramPacket;

import.DatagramSocket;

import.SocketException;

publicclassCollectPipeServer{

privatestaticfinalintDEFAULT_PORT=8888;

privatestaticfinalintBUFFER_LENGTH=2048;

publicstaticvoidmain(Stringargs[]){

intport=DEFAULT_PORT;

if(args.length>0)

port=Integer.parseInt(args[0]);

byte[]buffer=newbyte[BUFFER_LENGTH];

DatagramSocketaSocket=null;

try{

aSocket=newDatagramSocket(port);

System.out.printf("CollectPipelisteningonport%d...\n",port);

while(true){

DatagramPacketrequest=newDatagramPacket(buffer,

buffer.length);

aSocket.receive(request);

Stringmessage=newString(buffer,0,request.getLength());

System.out.println(message);

}

}catch(SocketExceptione){

System.out.println("Socket:

"+e.getMessage());

}catch(IOExceptione){

System.out.println("IO:

"+e.getMessage());

}finally{

if(aSocket!

=null)

aSocket.close();

}

}

}

 

答:

UDP报文中包括目的主机IP和报文长度,当它被发送后,网络中的路由器根据目的IP一级一级的转发,最后到达目的主机。

五.实验总结和体会

通过这次试验,让我对数据报通信的原理以及利用Java语言实现C/S下的UDP通信有了更深的理解,期待下次的进步,加油~

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2