您好,欢迎来到智榕旅游。
搜索
您的当前位置:首页网络协议实验报告 - 副本

网络协议实验报告 - 副本

来源:智榕旅游


网络协议分析实验报告

指导老师: 姓名: 学号:

第一题 传输与传播时延

实验目的是深入理解传输时延与传播时延的概念以及区别掌握传输时延与传

播时延的计算方法。

实验原理:实验原理:总时延=发送时延+传播时延+处理时延+排队时延 核心程序分析:

public class LineSimApplet extends Applet {

// 开始按钮

Button start = new Button(\"Start\"); //结束按钮

Button stop = new Button(\"Reset\");

// 特性列表

MyChoice length = new MyChoice(

new String[] { \"10 km\\"100 km\\"1000 km\" }, new double[] { 10E3,

100E3, 1E6 }, 3);

MyChoice rate = new MyChoice(new String[] { \"512 kps\\"10 Mbps\

\"100 Mbps\" }, new double[] { 512E3, 1E6, 10E6, 100E6 }, 2); MyChoice size = new MyChoice(new String[] { \"100 Bytes\Bytes\

\"1 kBytes\" }, new double[] { 8E2, 4E3, 8E3 }, 1);

// 模仿时间

Thread timerThread; TickTask timerTask;

boolean simulationRunning = false; // 通信线

Line myLine;

public void init() { try {

//设置背景颜色

setBackground(Color.white);

//添加标签到右边

add(new Label(\"Length\//把特性列表加进去 add(length);

//添加频率标签

add(new Label(\"Rate\//把频率特性列表加进去 add(rate);

//包大小的标签添加到右边

add(new Label(\"Packet size\//把尺寸特性列表加进去

}

add(size); // start

//添加开始按钮的监听事件

start.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) { //执行监听事件,开始仿真 launchSim(); } });

add(start);

// 新建停止按钮

Button stop = new Button(\"Reset\");

//添加监听事件

stop.addActionListener(new ActionListener() {

//覆盖监听事件列表,执行监听事件

public void actionPerformed(ActionEvent event) {

//停止仿真 stopSim();

// 清除绘制线

myLine.sendTime(0);

// 重绘清除线

LineSimApplet.this.repaint(); } });

add(stop);

// 画线

myLine = new Line(40, 50, 450, 10); } catch (Exception e) { e.printStackTrace(); }

//重写paint方法

public void paint(Graphics g) { update(g); // 消除闪烁:更新是重载 }

public void update(Graphics g) { //工作在一个画面以外的形象

/* 维类封装了一个组件的宽度和高度(整数精密)在一个单一的对象。 * 类是与某些属性的组件。几种方法中定义的组件类和LayoutManager界面返回一个维度对象。

通常情况下,值的非负整数的宽度和高度是。

* */

Dimension offDimension = getSize();

//创建图形

Image offImage = createImage(offDimension.width, offDimension.height);

//创建画笔

Graphics offGraphics = offImage.getGraphics(); //画出通信线

myLine.drawLine(offGraphics); //绘制发送按钮

offGraphics.setColor(Color.blue); //填充边框

offGraphics.fillRect(10, 40, 30, 30); //设置颜色

offGraphics.setColor(Color.black); //绘制字符串

offGraphics.drawString(\"Sender\//填充矩形

offGraphics.drawRect(10, 40, 30, 30); //绘制接收按钮

offGraphics.setColor(Color.blue);

offGraphics.fillRect(490, 40, 30, 30); //设置颜色

offGraphics.setColor(Color.black);

//绘制字符串

offGraphics.drawString(\"Receiver\//填充矩形

offGraphics.drawRect(490, 40, 30, 30);

//打印一句话 offGraphics

.drawString(\"Propagation speed : 2.8 x 10^8 m/sec\175, 105);

//显示画面以外的形象

g.drawImage(offImage, 0, 0, this); }

private void launchSim() { setupEnabled(false);

// 设置line

myLine.setup(length.getVal(), rate.getVal());

//发射数据包

myLine.emitPacket(size.getVal(), 0);

//设置时间

timerTask = new TickTask(1E-5, myLine.totalTime()); timerThread = new Thread(timerTask); // 开始仿真

simulationRunning = true;

//启动线程

timerThread.start(); } /**

* 停止发送 */

private void stopSim() { timerTask.endNow();

simulationRunning = false; setupEnabled(true); } /**

* 设置功能

* @param value */

public void setupEnabled(boolean value) { start.setEnabled(value); length.setEnabled(value); rate.setEnabled(value); size.setEnabled(value); }

// 绘制选项界面

class MyChoice extends Choice { private double vals[];

//构造函数

public MyChoice(String items[], double values[], int defaultValue) {

for (int i = 0; i < items.length; i++) { super.addItem(items[i]); }

vals = values;

select(defaultValue - 1); }

}

public double getVal() {

return vals[super.getSelectedIndex()]; } }

class TickTask implements Runnable {

private double counter;//当前计算的长度 private double length;//最大长度 private double tick;//标记

//重构构造函数

public TickTask(double t, double l) { length = l; tick = t; counter = 0; }

public void run() {

while (LineSimApplet.this.simulationRunning) { counter += tick; }

}

//通过当前计算的长度,计算发送事件的时间

LineSimApplet.this.myLine.sendTime(counter); //重画

LineSimApplet.this.repaint(); if (counter >= length) {

//如果大于最大长度,那么清除数据包

LineSimApplet.this.myLine.clearPackets();

//线程挂起

LineSimApplet.this.timerThread.suspend(); }

try {

//线程暂停50毫秒

LineSimApplet.this.timerThread.sleep(50); } catch (Exception e) { }

public void endNow() { length = counter; } }

class Line { // 图形变量

private int gX;//起点横坐标 private int gY;//起点纵坐标 private int gWidth;//线的宽度 private int gHeight;//线的高度 //特征变量

final double celerity = 2.8E+8; private double length; private double rate; //模拟变量

private double time;

private Packet myPacket;

//线的构造函数

public Line(int x, int y, int w, int h) { // graphic init gX = x; gY = y;

gWidth = w; gHeight = h; }

// 初始化函数

public void setup(double l, double r) { length = l; rate = r; }

// 计算发送事件的函数

void sendTime(double now) { time = now; // update time removeReceivedPackets(now); }

// 发射数据包的函数

void emitPacket(double s, double eT) { myPacket = new Packet(s, eT);

}

//清楚已经接收的数据包

private void removeReceivedPackets(double now) { if (!(myPacket == null)) {

if (now > myPacket.emissionTime + (myPacket.size / rate) + length

* celerity) { clearPackets(); } } }

// 清除数据包

public void clearPackets() { myPacket = null; }

//计算总时间

public double totalTime() {

double emmissionTime = (myPacket.size / rate); double onLineTime = (length / celerity); return (emmissionTime + onLineTime); }

//画线函数

public void drawLine(Graphics g) { g.setColor(Color.white);

g.fillRect(gX, gY + 1, gWidth, gHeight - 2); g.setColor(Color.black);

g.drawRect(gX, gY, gWidth, gHeight); g.setColor(Color.red);

g.drawString(timeToString(time), gX + gWidth / 2 - 10, gY + gHeight

+ 15); drawPackets(g); }

// 绘制数据包函数

private void drawPackets(Graphics g) {

if (!(myPacket == null)) { double xfirst; double xlast;

// 计算时间的单位

xfirst = time - myPacket.emissionTime; xlast = xfirst - (myPacket.size / rate); //计算位置

xfirst = xfirst * celerity * gWidth / length; xlast = xlast * celerity * gWidth / length; if (xlast < 0) { xlast = 0; }

if (xfirst > gWidth) { xfirst = gWidth; }

//开始绘制

g.setColor(Color.red);

g.fillRect(gX + (int) (xlast), gY + 1, (int) (xfirst -

gHeight - 2);

xlast), } }

// 时间变成字符串显示的函数

设定好各个参数之后按Start键,分组即开始传输,如下图所示:链路长度为1000km、传输速率为1 Mb/s,分组长度为100B。

改变length:

改变rate:

第二题 排队时延和丢包

此实验的目的是为了深入了解时延与丢包的概念和关系,核心程序代码分析如下: Button start=new Button(\"Start\"); //设置开始按钮

Button reset=new Button(\"Reset\"); //设置结束按钮

MyChoice emitRate=new MyChoice(new String[] {\"500 packet/s\{2E-3,3E-3},1);

MyChoice processorRate=new MyChoice(new String[] {\"1000 packet/s\packet/s\ //特征列表

SimTimer myTimer; //模仿时间

public void init() { try {

myTimer=new SimTimer(1E-4,1000,this); setBackground(Color.white);

//设置背景颜色为白色

add(new Label(\"Emission rate\ //添加标签到右边 add(emitRate);

//把频率特性列表加进去

add(new Label(\"Transmission rate\

//添加频率标签 add(processorRate);

start.addActionListener(new ActionListener() //添加开始按钮的监听事件 {

public void actionPerformed (ActionEvent event) {

//执行监听事件,开始仿真 emitRate.setEnabled(false); //设置line

processorRate.setEnabled(false); start.setEnabled(false);

//停止线程

myTimer.launchSim(emitRate.getVal(), processorRate.getVal()); } });

add(start);

//新建停止按钮

reset.addActionListener(new ActionListener() {

//添加监听事件

public void actionPerformed (ActionEvent event) {

//停止仿真

emitRate.setEnabled(true);

processorRate.setEnabled(true); start.setEnabled(true);

myTimer=new SimTimer(1E-4,1000,QueueSim.this); } });

//初始化页面

public void update (Graphics g) {

//工作在一个画面以外的形象,维类封装了一个组件的宽度和高度(整数精密)在一个单一的对象。类是与某些属性的组件。几种方法中定义的组件类和LayoutManager界面返回一个维度对象。

//work on a offscreen image

Dimension offDimension = getSize();

//创建图形

Image offImage = createImage(offDimension.width, offDimension.height); //创建画笔

Graphics offGraphics = offImage.getGraphics(); //画出通信线

myTimer.draw(offGraphics); //绘制发送按钮

g.drawImage(offImage, 0, 0, this); }

public void run() {

while (true) {

dropper1.setTime(time); sender1.setTime(time); line1.setTime(time); queue1.setTime(time); proc1.setTime(time); line2.setTime(time); target.repaint(); time+=tic;

try {timerThread.sleep(50);} catch (Exception e) { };//线程暂停五十毫秒 } }

//线程暂时挂起五十毫秒

工作在一个画面以外的形象设定发送速率为500packet/s传输速率为500packet/s,既

传输速率等于发送速率时,一般不会发生丢包现象。例如当进行下图所示的实验时,传输时间为80ms,传输分组为34个,丢包率为零。

设定发送速率为500 packet/s,传输速率为350 packet/s,即发送速率大于传输速率,分组一般在路由器缓存中会产生排队现象,从而导致排队时延。由于缓存器容量(队列)是有限的,当到达的分组发现排列队列已满时,将会被丢弃。

设发送速率为500 packet/s,传输速率为1000 packet/s,即当发送速率比传输速率小得多时,发送分组将被及时传输出去,路由器中不会有数据分组排队,因此也不会产生排队时延。

端到端时延中最为复杂和有趣的成分就是排队时延。在这个程序中,可以设置分组发送速率和链路传输速率,你将会看到分组到达和为接受服务而排队。当队列变满时,将会发生分组溢出,也就是丢包。

当发送端与接收端速率相同时,队列将会变得毫无负担,但当程序执行很长一段时间队列同样会发生溢出。

第七题 选择性重传协议演示

UDP提供的是无连接的、不可靠的数据传送方式,是一种尽力而为的数据交付服务。 滑动窗口技术

协议中规定,对于窗口内未经确认的分组需要重传。这种分组的数量最多可以等于发送窗口的大小,即滑动窗口的大小n减去1(因为发送窗口不可能大于(n-1),起码接收窗口要大于等于1)。

核心程序分析:

final int window_len_def = 8; final int pack_width_def = 10; final int pack_height_def = 30; final int h_offset_def = 100; final int v_offset_def = 50; final int v_clearance_def = 300; final int total_packet_def = 20; final int time_out_sec_def = 30; //对不同的图形进行定义

final Color unack_color = Color.red;

//获取packet_width参数值到strPackWd

strWinLen = getParameter(\"window_length\"); 发生异常执行catch (Exception e) try {

if (strWinLen != null) window_len = Integer.parseInt(strWinLen);

} catch (Exception e) {}

//上面调用getParameter发生错误的时候没有返回值时采用下面的定义的默认值 window_len = (window_len > 0) ? window_len : window_len_def; pack_width = (pack_width > 0) ? pack_width : pack_width_def;

send = new Button(\"Send New\"); //创建主线程,开始线程 public void start() {

if (gbnThread==null)

gbnThread = new Thread(this); gbnThread.start(); //定义图形化界面的按键

//检查分组数据包是否在传输,如果开始传输则发送从0到total_packet的数据包 if (onTheWay(sender)) {

for (int i=0; iif (sender[i].on_way)

if (sender[i].packet_pos < (v_clearance-pack_height)) sender[i].packet_pos+=5;

//如果分组包正在传输到目的地,收到分组包后,接收方返回一个acknowledgement确认报文通知发送方此分组已收到 else if (sender[i].packet_ack) {

sender[i].reached_dest = true;

if (check_upto_n(i))

{ // packets are received.

sender[i].packet_pos = pack_height+5; sender[i].packet_ack = false;

//正确接收,窗口向右移动

statusMsg = \"Packet \"+i+\" received. Acknowledge sent.\";

//如果传送失败,没有返回确认报文,则不返回确认报文 sender[i].on_way = false;

statusMsg = \"Packet \"+i+\" received. No acknowledge sent.\";

//一下为图形化操作的定义 //Send按下的时候

if (cmd == \"rdt\" && nextseq < base+window_len) //加速按键按下的时候 else if (cmd == \"fast\") //图形画定义

public void update(Graphics g)

//运行图形化界面演示,发送分组数据包

Kill其中的一个分组数据包,模拟在传输过程中的丢包事件

已接受到的分组返回确认的acknowledgement

其中未接受到的分组没有返回确认的报文

等会设定事件后,发送方选择重传

重传结束,发送方继续发送分组

继续传输

最后传输完毕。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- zrrp.cn 版权所有 赣ICP备2024042808号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务