实验目的
编程模拟实现GBN可靠传输软件
实验内容
C++ 程序模拟实现Go-Back-N可靠数据传输,需要编写一个发送端程序和一个测试端程序来模拟传输过程
具体流程
-
编写发送端程序,调用库实现socket连接,然后主要实现滑动窗口,接收ACK确认帧和超时重传的功能
-
编写接收端,调用监听一个端口,因为在本地实验,要模拟丢包的过程,使用随机数模拟出百分之十的丢包率。编写丢包前后不同的ACK确认帧逻辑
关键代码
发送端,先建立连接,然后接收用户在命令行的输入,获取要发送的消息和滑动窗口。然后开始按照GBN协议发送,发送完一帧后就滑动窗口往下。发送完一帧的同时还会开启一个计时器,计时器延时一秒钟,没有收到ACK就会从超时的位置开始重传
cpp
asio::io_service io_service;
tcp::socket socket(io_service);
socket.connect(tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 12345));
std::string message;
std::cout << "Input message: ";
std::getline(std::cin, message);
int window_size;
std::cout << "Window size: ";
std::cin >> window_size;
std::vector<char> window(window_size);
int base = 0;
int next_seq_num = 0;
asio::steady_timer timer(io_service);
timer.expires_from_now(std::chrono::seconds(1));
while (base < message.size()) {
while (next_seq_num < base + window_size && next_seq_num < message.size()) {
window[next_seq_num % window_size] = message[next_seq_num];
char seq_num_char = static_cast<char>(next_seq_num);
asio::write(socket, asio::buffer(&seq_num_char, 1));
asio::write(socket, asio::buffer(&window[next_seq_num % window_size], 1));
std::cout << "Seq_num: " << next_seq_num << " Send: " << window[next_seq_num % window_size] << std::endl;
next_seq_num++;
}
char ack;
asio::async_read(socket, asio::buffer(&ack, 1), [&](const asio::error_code& error, std::size_t length) {
if (!error) {
for (int i = 0; i < 25; i++) {
std::cout << " ";
}
std::cout << "Receive ACK: " << static_cast<int>(ack) << std::endl;
base = ack + 1;
if (base == next_seq_num) {
timer.cancel();
}
else {
timer.expires_from_now(std::chrono::seconds(1));
}
}
else {
std::cout << "Error reading ACK: " << error.message() << std::endl;
}
});
timer.async_wait([&](const asio::error_code& error) {
if (!error) {
std::cout << "Timeout resend from: " << base << std::endl;
for (int i = base; i < next_seq_num; i++) {
char seq_num_char = static_cast<char>(i);
asio::write(socket, asio::buffer(&seq_num_char, 1));
asio::write(socket, asio::buffer(&window[i % window_size], 1));
}
}
});
io_service.run();
io_service.reset();
}
接收端,先开启端口的监听,然后开始接收消息,中间模拟随机百分之十的丢包率,如果发生丢包只会发送重复最后收到的连续的帧。还有一段处理错误的代码,接收结束消息,安全退出
cpp
asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 12345));
tcp::socket socket(io_service);
acceptor.accept(socket);
srand(time(0));
int expected_seq_num = -1;
while (true) {
char seq_num_char;
char data;
asio::error_code error;
asio::read(socket, asio::buffer(&seq_num_char, 1), error); // read sequence number
asio::read(socket, asio::buffer(&data, 1), error); // read data
if (error) {
if (error == asio::error::eof) {
std::cout << "\nAll message have been received, connection closed" << std::endl;
}
else {
std::cout << "Error reading: " << error.message() << std::endl;
}
break;
}
int seq_num = static_cast<int>(seq_num_char);
if (expected_seq_num == -1) {
int random_num = rand() % 10;
if (random_num == 0) {
std::cout << "Loss Seq_num: " << seq_num<< std::endl;
expected_seq_num = seq_num;
continue;
}
std::cout << "Seq_num: " << seq_num << " Receive: " << static_cast<char>(data) << std::endl;
}
else {
std::cout << "Seq_num: " << seq_num << " Receive: " << static_cast<char>(data) << std::endl;
if (seq_num == expected_seq_num) {
expected_seq_num = -1;
}
else {
seq_num = expected_seq_num - 1;
}
}
asio::write(socket, asio::buffer(&seq_num, 1));
for (int i = 0; i < 25; i++) {
std::cout << " ";
}
std::cout << "Send ACK: " << seq_num << std::endl;
}
运行示例
因为有两个程序运行,所以我们打开两个终端。先编译链接生成发送端和接收端的程序
先再一个终端中运行接收端,程序,监听对应的端口。然后再在另外一个终端中运行发送端程序,输入要发送的信息,和滑动窗口的大小。这里我设置的要发送的信息为"thiisatestmessage",滑动窗口大小为5
然后程序就会开开始模拟,可以看到发送端一次性快速发送完了窗口中的内容,但是接收端在一个一个接收到只之后才会发送对应帧的ACK。可以看到我们在模拟的过程中丢失了4号和15号两个帧,这两个帧丢失后的ACK都只会重复发送丢失帧之前的最后一个ACK,然后发送端超时之后就会从对应的位置开始重新发送
完整代码
BJTU_CS_Learning/computernetwork at main · JJLi0427/BJTU_CS_Learning (github.com)