帮我完善一下代码:使用FlowID=1、权重=1和数据包大小1024启动发送端。几秒钟后,使用FlowID=2、权重=1和数据包大小512启动另一个发送器。再过几秒钟,使用FlowID=3、权重=2和数据包大小1024启动第三个发送方。具体要求:1.接收者在"sender.py"中写三个线程,并且规定有:source ip,3个线程开启的source port,destination ip,3个destination port,router ip,发送的信息可以随意生成。2.在router.py中就是接收到sender发送的数据并且根据WFQ算法调度接收到的数据包并转发给receiver。3.在"receiver.py"中,接收消息并进行数据统计,每次完成一个数据包时记录时间、流ID和数据包的大小,最后画出每个流上接收到的总字节与时间的图表。
时间: 2024-03-16 09:44:05 浏览: 130
好的,以下是您需要的代码。请注意,这只是一个基本的实现,您需要根据您的需求进行修改和优化。
sender.py:
```python
import socket
import time
import random
import threading
def send_data(flow_id, weight, packet_size, dest_ip, dest_port):
# 创建socket
send_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 发送数据
while True:
data = ''.join(random.choices('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', k=packet_size-4))
data = str(flow_id) + data
send_sock.sendto(data.encode(), (dest_ip, dest_port))
time.sleep(1/weight)
def main():
# 设置参数
flow_id_list = [1, 2, 3]
weight_list = [1, 1, 2]
packet_size_list = [1024, 512, 1024]
ip_list = ["127.0.0.1", "127.0.0.1", "127.0.0.1"]
port_list = [9999, 10000, 10001]
# 启动发送线程
threads = []
for i in range(3):
t = threading.Thread(target=send_data, args=(flow_id_list[i], weight_list[i], packet_size_list[i], ip_list[i], port_list[i]))
t.start()
threads.append(t)
for t in threads:
t.join()
if __name__ == '__main__':
main()
```
router.py:
```python
import socket
import threading
import queue
def wfq(queue_list, weight_list):
"""
WFQ算法
:param queue_list: 队列列表
:param weight_list: 权重列表
:return: 返回被选中的队列索引
"""
min_weight = min(weight_list)
index_list = [i for i, j in enumerate(weight_list) if j == min_weight]
min_len = float("inf")
selected_index = -1
for index in index_list:
if len(queue_list[index]) < min_len:
min_len = len(queue_list[index])
selected_index = index
return selected_index
def receiver(recv_sock, queue_list, weight_list, flow_size_dict):
"""
接收数据并进行WFQ
:param recv_sock: 接收端socket
:param queue_list: 队列列表
:param weight_list: 权重列表
:param flow_size_dict: 流ID与接收到的总字节数的字典
"""
while True:
data, addr = recv_sock.recvfrom(1024)
flow_id = int(data[:1])
if flow_id not in flow_size_dict:
flow_size_dict[flow_id] = 0
index = wfq(queue_list, weight_list)
queue_list[index].put((data, addr))
def sender(send_sock, queue_list):
"""
发送数据
:param send_sock: 发送端socket
:param queue_list: 队列列表
"""
while True:
for queue in queue_list:
if not queue.empty():
data, addr = queue.get()
send_sock.sendto(data, addr)
def main():
# 创建socket
recv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
recv_sock.bind(("127.0.0.1", 9999))
send_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 创建队列
queue_list = [queue.Queue() for i in range(3)]
# 设置权重
weight_list = [1, 1, 2]
# 创建流ID与接收到的总字节数的字典
flow_size_dict = {}
# 启动接收线程
recv_thread = threading.Thread(target=receiver, args=(recv_sock, queue_list, weight_list, flow_size_dict))
recv_thread.start()
# 启动发送线程
send_thread = threading.Thread(target=sender, args=(send_sock, queue_list))
send_thread.start()
if __name__ == '__main__':
main()
```
receiver.py:
```python
import socket
import time
import matplotlib.pyplot as plt
def plot(flow_size_dict):
"""
绘制图表
:param flow_size_dict: 流ID与接收到的总字节数的字典
"""
x = []
y = []
for flow_id, size in flow_size_dict.items():
x.append(time.time() - start_time)
y.append(size)
plt.plot(x, y)
plt.xlabel("Time (s)")
plt.ylabel("Total Bytes Received")
plt.show()
def main():
# 创建socket
recv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
recv_sock.bind(("127.0.0.1", 10001))
# 创建流ID与接收到的总字节数的字典
flow_size_dict = {}
# 记录开始时间
global start_time
start_time = time.time()
# 接收数据并统计
while True:
data, addr = recv_sock.recvfrom(1024)
flow_id = int(data[:1])
size = len(data) - 1
if flow_id not in flow_size_dict:
flow_size_dict[flow_id] = 0
flow_size_dict[flow_id] += size
if len(flow_size_dict) == 3:
plot(flow_size_dict)
if __name__ == '__main__':
main()
```
在这个例子中,sender.py启动三个线程分别发送不同流ID、权重和数据包大小的数据包到router.py,router.py根据WFQ算法调度接收到的数据包并转发到receiver.py,receiver.py接收数据包并统计每个流ID接收到的总字节数,然后绘制每个流上接收到的总字节与时间的图表。您可以根据需要修改参数或添加更多功能。
阅读全文