计算机网络课程实验3——编程实现可靠数据传输原理 Go-Back-N

计算机网络课程实验3——编程实现可靠数据传输原理 Go-Back-N,第1张

计算机网络课程实验3——编程实现可靠数据传输原理 Go-Back-N

实验目的: 运用各种编程语言实现基于 Go-Back-N 的可靠数据传输软件。
实验意义: 通过本实验,使学生能够对可靠数据传输原理有进一步的理解和掌握。

实验背景:

实验步骤:

1,选择合适的编程语言编程实现基于 Go-Back-N 的可靠数据传输软件。
2,在实际网络环境或模拟不可靠网络环境中测试和验证自己的可靠数据传输软件。

实验代码如下(分为发送端和接收端): 发送端:
# Sender.py
import time, socket, sys

def decimalToBinary(n):  
    return n.replace("0b", "")

def binarycode(s):
    a_byte_array = bytearray(s, "utf8")

    byte_list = []

    for byte in a_byte_array:
        binary_representation = bin(byte)
        byte_list.append(decimalToBinary(binary_representation))

    #print(byte_list)
    a=""
    for i in byte_list:
        a=a+i
    return a

print("nWelcome to Chat Roomn")
print("Initialising....n")
time.sleep(1)

s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")n")
name = input(str("Enter your name: "))
           
s.listen(1)
print("nWaiting for incoming connections...n")
conn, addr = s.accept()
print("Received connection from ", addr[0], "(", addr[1], ")n")

s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat roomnEnter [e] to exit chat roomn")
conn.send(name.encode())

while True:
    #模拟信息输入
    message = input(str("Me : "))
    conn.send(message.encode())
    if message == "[e]":
        message = "Left chat room!"
        conn.send(message.encode())
        print("n")
        break
    message=binarycode(message)
    f=str(len(message))
    conn.send(f.encode())
   
    #模拟窗口移动
    i=0
    j=0
    j=int(input("Enter the window size -> "))
    
   
    b=""
   
    j=j-1
    f=int(f)
    k=j
    while i!=f:
        while(i!=(f-j)):
            conn.send(message[i].encode())
            b=conn.recv(1024)
            b=b.decode()
            print(b)
            if(b!="ACK Lost"):
                time.sleep(1)
                print("Acknowledgement Received! The sliding window is in the range "+(str(i+1))+" to "+str(k+1)+" Now sending the next packet")
                i=i+1
                k=k+1
                time.sleep(1)
            else:
                time.sleep(1)
                print("Acknowledgement of the data bit is LOST! The sliding window remains in the range "+(str(i+1))+" to "+str(k+1)+" Now Resending the same packet")
                time.sleep(1)
        while(i!=f):
            
            conn.send(message[i].encode())
            b=conn.recv(1024)
            b=b.decode()
            print(b)
            if(b!="ACK Lost"):
                time.sleep(1)
                print("Acknowledgement Received! The sliding window is in the range "+(str(i+1))+" to "+str(k)+" Now sending the next packet")
                i=i+1
                time.sleep(1)
            else:
                time.sleep(1)
                print("Acknowledgement of the data bit is LOST! The sliding window remains in the range "+(str(i+1))+" to "+str(k)+" Now Resending the same packet")
                time.sleep(1)
接收端:
# Receiver.py
import time, socket, sys
import random

print("nWelcome to Chat Roomn")
print("Initialising....n")
time.sleep(1)

s = socket.socket()
shost = socket.gethostname()
ip = socket.gethostbyname(shost)
print(shost, "(", ip, ")n")
host = input(str("Enter server address: "))
name = input(str("nEnter your name: "))
port = 1234
print("nTrying to connect to ", host, "(", port, ")n")
time.sleep(1)
s.connect((host, port))
print("Connected...n")

s.send(name.encode())
s_name = s.recv(1024)
s_name = s_name.decode()
print(s_name, "has joined the chat roomnEnter [e] to exit chat roomn")

while True:

    m=s.recv(1024)
    m=m.decode()
    k=s.recv(1024)
    k=k.decode()
    k=int(k)
    i=0
    a=""
    b=""
    f=random.randint(0,1)
    message=""
    while i!=k:
       
       //模拟丢包
       f=random.randint(0,1)
       if(f==0):
          b="ACK Lost"
          message = s.recv(1024)
          message = message.decode()
          s.send(b.encode())
         
       elif(f==1):
          b="ACK "+str(i)
          message = s.recv(1024)
          message = message.decode()
          
          s.send(b.encode())
          a=a+message
          i=i+1
          
       
    
    print("The message received is :", m)

代码实现效果:
因为我使用的python编译器是sublime text,这个编译器不支持输入函数,所以使用命令行执行上述文件代码。
发送方:

接收方:

接下来,双方建立链接:
接收方:

发送方:

此时,双方已经完成链接的建立,在发送方输入信息,并且设置好窗口的长度,进行信息的传递:
发送方:在完成信息设定之后就会开始进行数据的传输,因为考虑到这次的实验是本机传递,不可能丢包,所以在程序自动设定了50%概率丢包判定。

接收方:可以准确收到发送方的输入信息。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5721220.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存