请帮帮我;我不知道如何处理Python中的数据包速率损失从0-10%。
我需要编写sender.py和receiver.py来连接频道并修改频道,以便它可以发送图片。
以下是channel.py的代码:
#
import random
import socket
from socket import *
#Vary the channel loss rate between 0-10
lossRate = 0
#Create UDP sockets for sender and receiver
senderSocket = socket(AF_INET, SOCK_DGRAM)
receiverSocket = socket(AF_INET, SOCK_DGRAM)
# Assign IP address and port numbers to sockets
senderSocket.bind(('127.0.0.1', 5001))
receiverSocket.bind(('127.0.0.1', 5002))
receiverMessage, receiverAddress = receiverSocket.recvfrom(1600)
print "Receiver ready"
while True:
# Receive the messages from sender
senderMessage, senderAddress = senderSocket.recvfrom(1600)
#forward the frame to the receiver with a random loss
rand = random.randint(0,99)
if rand > lossRate:
receiverSocket.sendto(senderMessage, receiverAddress)
#receive the ACK from receiver
receiverMessage, receiverAddress = receiverSocket.recvfrom(1600)
#forward the ACK to the sender with a random loss
rand = random.randint(0,99)
if rand > lossRate:
senderSocket.sendto(receiverMessage, senderAddress)