• 大小: 8KB
    文件类型: .java
    金币: 2
    下载: 1 次
    发布日期: 2021-06-14
  • 语言: Java
  • 标签: Go-Back-N  

资源简介

运用编程语言实现基于Go-Back-N 的可靠数据传输软件

资源截图

代码片段和文件信息

//package protocol;
import java.net.*;
import java.util.*;
import java.io.*;

@SuppressWarnings(“unused“)
public class GBN extends Thread {
static void senddelay(int x int y) throws InterruptedException {
if (x == 1) {
sleep(300);
System.out.println(“发送数据分组“ + y + “时发生延迟:300毫秒!“);
} else if (x == 2) {
sleep(750);
System.out.println(“发送数据分组“ + y + “时发生延迟:750毫秒!“);
} else if (x == 3) {
sleep(1200);
System.out.println(“发送数据分组“ + y + “时发生延迟:1200毫秒!“);
} else if (x == 4) {
sleep(2500);
System.out.println(“发送数据分组“ + y + “时发生延迟:2500毫秒!“);
} else if (x == 5) {
sleep(3000);
System.out.println(“发送数据分组“ + y + “时发生延迟:3000毫秒!“);
} else
;
}

static void OutputWindow(Sender s) {
for (int k = 0; k < s.windowsize; k++) {
// 表示至少成功发送并确认了一个数据分组
s.windowsign[k]++;
// 这种情况下滑动窗口向前移动!
}
System.out.println();
System.out.println(“当前窗口内的分组情况为:“);
// 显示当前窗口内数据包情况
for (int p = 0; p < s.windowsize; p++) {
if (s.windowsign[p] <= 6)
if (s.sent[s.windowsign[p]] == 0) {
System.out.println(“第“ + p + “号窗口里面存放的是序号为“
+ s.windowsign[p] + “的待确认的数据分组!“);
} else
System.out.println(“第“ + p + “号窗口里面存放的是序号为“
+ s.windowsign[p] + “的待发送的数据分组!“);
else
System.out.println(“第“ + p + “号窗口已经空了,并且后续窗口、发送方没有要发送的数据分组了!“);
}
System.out.println();
}

public static void main(String[] args) throws IOException
InterruptedException {
Sender s = new Sender();
Receiver re = new Receiver();
s.start();// 发送端启动
re.run(s);// 接收端启动
sleep(1000);// 延迟处理
for (int i = 0; i <= s.sign.length; i++) {
while (i > s.localack + 1) {
// 尚有未确认的数据包,重发!
System.out.println(“发送方开始重新发送序号为“ + (s.localack + 1) + “的数据分组“);
int ran = new Random().nextInt(4);
int randelay = new Random().nextInt(6);
s.time();
senddelay(randelay s.localack + 1);// 设置随机值,模拟数据传输延迟
if (ran != 1) {
re.receive(s.localack + 1 s);// 设置随机值,模拟数据丢包的过程
if (s.localack >= 0)
if (s.acksign[s.localack] == 0) {
if (s.localack + 3 <= 6)
s.sent[s.localack + 3] = 0;
OutputWindow(s);
}
} else
System.out.println(“序号为“ + (s.localack + 1)
+ “的分组在传给接收方途中发生了丢包!“);
}
if (i != s.sign.length) {
if (i == 0) {
System.out.println();
System.out.println(“发送方:现在发送序号为0-2的数据分组“);
s.sent[0] = 0;
s.sent[1] = 0;
s.sent[2] = 0;
System.out.println();
int ran1 = new Random().nextInt(4);
int randelay1 = new Random().nextInt(6);
s.time();// 计时开始(2秒时间)
senddelay(randelay1 0);// 设置随机值,模拟数据传输延迟
if (ran1 != 1) {
re.receive(s.sign[0] s);// 设置随机值,模拟数据丢包过程
if (s.acksign[0] == 0) {
s.sent[3] = 0;
s.acknowledged[0] = 0;
OutputWindow(s);
}
} else
System.out.println(“序号为“ + “0“ + “的分组在传给接收方途中发生了丢包!“);
i++;
int ran2 = new Random().nextInt(4);
int randelay2 = n

评论

共有 条评论

相关资源