![UDP和TCP网络实验报告_第1页](http://file1.renrendoc.com/fileroot_temp2/2020-11/21/9a362ff0-08af-4c3d-9e52-03b4bdd3196e/9a362ff0-08af-4c3d-9e52-03b4bdd3196e1.gif)
![UDP和TCP网络实验报告_第2页](http://file1.renrendoc.com/fileroot_temp2/2020-11/21/9a362ff0-08af-4c3d-9e52-03b4bdd3196e/9a362ff0-08af-4c3d-9e52-03b4bdd3196e2.gif)
![UDP和TCP网络实验报告_第3页](http://file1.renrendoc.com/fileroot_temp2/2020-11/21/9a362ff0-08af-4c3d-9e52-03b4bdd3196e/9a362ff0-08af-4c3d-9e52-03b4bdd3196e3.gif)
![UDP和TCP网络实验报告_第4页](http://file1.renrendoc.com/fileroot_temp2/2020-11/21/9a362ff0-08af-4c3d-9e52-03b4bdd3196e/9a362ff0-08af-4c3d-9e52-03b4bdd3196e4.gif)
![UDP和TCP网络实验报告_第5页](http://file1.renrendoc.com/fileroot_temp2/2020-11/21/9a362ff0-08af-4c3d-9e52-03b4bdd3196e/9a362ff0-08af-4c3d-9e52-03b4bdd3196e5.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、计算机网络实验报告-TCP/UDP网络通信班级: 姓名: 学号: 目录1:实验题目:基于UDP/TCP的通信程序开发22:实验目的:23.实验报告内容:23.1 UDP通信程序23.1.1接收端:33.1.2发送端:43.1.3实验结果:53.2UDP群聊程序:53.2.1UDP群聊源代码53.2.2实验结果:83.3TCP通信83.3.1服务端83.3.2客户端103.3.3实验结果114实验总结121:实验题目:基于UDP/TCP的通信程序开发2:实验目的:开发TCPUDP协议应用程序,掌握网络应用程序的工作原理。通过本实验,深入理解TCP和UDP协议的异同点,了解网络协议的工作过程,学会
2、网络通讯编程的基本方法,能够编制网络应用程序。3.实验报告内容:3.1 UDP通信程序3.1.1接收端:package .udp;import java.io.IOException;import .DatagramPacket;import .DatagramSocket;import .SocketException;public class UdpReceiveDemo1 /* * param args * throws IOException */public static void main(String ar
3、gs) throws IOException System.out.println(UDP接收端开启:);DatagramSocket ds =new DatagramSocket(10000);byte buf=new byte1024;DatagramPacket dp =new DatagramPacket(buf, buf.length);ds.receive(dp);String ip=dp.getAddress().getHostAddress();int port =dp.getPort();byte data =dp.getData();String text =new Str
4、ing(data, 0, dp.getLength();System.out.println(ip-+ip+,port-+port+,text-+text);ds.close();3.1.2发送端:package .udp;import java.io.IOException;import .DatagramPacket;import .DatagramSocket;import .InetAddress;public class UdpSendDemo1 /* * param args * throws IOExcep
5、tion */public static void main(String args) throws IOException /* * 需求:通过UDP发送一段信息给指定主机 */System.out.println(UDP发送端开启:);DatagramSocket ds =new DatagramSocket();String str =Hi ,我是通过UDP发过来的;byte buf =str.getBytes();DatagramPacket dp =new DatagramPacket(buf, buf.length,InetAddress.getByName(115.154.71.
6、98),10000);ds.send(dp);ds.close();3.1.3实验结果:3.2UDP群聊程序: 3.2.1UDP群聊源代码package .udp;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import .DatagramPacket;import .DatagramSocket;import .InetAddres
7、s;import .SocketException;public class UdpChatDemo1 /* * param args * throws SocketException */public static void main(String args) throws SocketException /* * 群聊程序 多线程:一线程负责收,一负责发 定义两个线程任务 */ 1.创建接收端 和发送端DatagramSocket send = new DatagramSocket(1200);DatagramSocket rece = new DatagramSocket
8、(10000);new Thread(new Send(send).start();new Thread(new Rece(rece).start();public static class Send implements Runnable private DatagramSocket ds;public Send(DatagramSocket ds) super();this.ds = ds;public void run() try BufferedReader reader = new BufferedReader(new InputStreamReader(System.in);Str
9、ing line = null;while (line = reader.readLine() != null) if (over.equals(line)break;byte buf = line.getBytes();DatagramPacket dp = new DatagramPacket(buf, buf.length,InetAddress.getByName(8), 10000);ds.send(dp);ds.close(); catch (Exception e) / TODO: handle exceptionpublic static class R
10、ece implements Runnable private DatagramSocket ds;public Rece(DatagramSocket ds) super();this.ds = ds;public void run() try byte buf = new byte1024;while (true) DatagramPacket dp = new DatagramPacket(buf, buf.length);ds.receive(dp);String ip = dp.getAddress().getHostAddress();String text = new Strin
11、g(dp.getData();if (over.equals(text) System.out.println(ip + .离开了);System.out.println(ip +: +text); catch (Exception e) / TODO: handle exception3.2.2实验结果:3.3TCP通信3.3.1服务端package .TCP;import java.io.IOException;import java.io.InputStream;import .ServerSocket;import .Socke
12、t;public class ServerDemo1 /* * 创建一个服务端,已接受数据 * param args * throws IOException */public static void main(String args) throws IOException System.out.println(服务端启动啦);/1.创建socket服务端对象,并监听一个端口ServerSocket ss=new ServerSocket(10000);/2.获取客户端对象,和指定的客户度进行通信Socket s =ss.accept();/获取客户端IPString ip =s.getIne
13、tAddress().getHostAddress();System.out.println(ip-+ip+connected);/3.有了socket就可以获取其中的流InputStream in =s.getInputStream();byte buf =new byte1024;int len=buf.length;String text =new String(buf, 0, len);System.out.println(text);/关闭资源,先关闭客户端s.close();ss.close();/关闭服务端3.3.2客户端package .TCP;imp
14、ort java.io.IOException;import java.io.OutputStream;import .Socket;import .UnknownHostException;public class ClientDemo1 /* * 定义一个TCP的客户端 * param args * throws IOException * throws UnknownHostException */public static void main(String args) throws UnknownHostException, IOException Sy
15、stem.out.println(客户端启动啦);/1.创建TCP客户端对象,必须要有socket服务/客户端通常已建立,就需要连接。因为这是面向连接的协议Socket s =new Socket(8,10000);/2.连接一旦建立就形成了数据传输通道,这个通道就是IO流,这个IO流由socket建立/所以称之为Socket IO流。OutputStream out =s.getOutputStream();/3 使用输出流对象将数据写入out.write(我是通过TCP传过来的.getBytes();/4关闭资源s.close();3.3.3实验结果4实验总结TCP与UDP的区别主要体现在以下几点:1.基于连接与无连接2.对系统资源的要求(TCP较多,UDP少)3.UDP程序结构较简单4.流模式与数据报模式5.TCP保证数据正确性,UDP可能丢包6.TCP保证数据顺序,UDP不保
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 专家评审意见范文(篇一)
- 与照相馆的合作合同范本
- 制造合同范本大全
- 医疗纠纷预防范文
- 绿化防治病虫害措施方案
- 沪教版一年级上册数学期末测试卷及参考答案ab卷
- 2024年三年级下册数学期末测试卷标准卷
- 2024年小学五年级下册数学期末测试卷带答案(黄金题型)
- 凯迪拉克上牌合同范本
- 小学分数乘法、除法练习题附答案【培优a卷】
- 全国实验教学说课比赛公开课一等奖市赛课获奖课件
- 建设工程总承包计价规范
- 应征入伍政策
- 部编版语文五年级下册第八单元教案
- 河南三支一扶笔试真题
- 2023年05月北京市大兴区公开招考社区工作者笔试题库含答案解析
- 红十字救护员获证培训-理论考试题目-含答案
- 学物明理@实验智慧树知到答案章节测试2023年内蒙古民族大学
- 浙江省宁波市镇海区2023年七年级下学期数学期中试卷【及参考答案】
- 高速铁路工程施工质量验收标准检验批示例电子版表格
- 经闭孔无张力尿道中断悬吊术
评论
0/150
提交评论