簡體   English   中英

UDP 插座帶順序檢查

[英]UDP socket with sequence check

我需要構建一個特定的解決方案,其中客戶端將 UDP 流量發送到另一個 IP 並選擇檢查數據包序列(亂序檢測)。

數據傳輸很清晰,我可以正常工作,但是我不清楚檢測亂序幀的序列號。

我是否必須向接收者發送 litteraly 數字,保存到文件中,然后 litteraly 檢查序列? 那會是什么樣子?

客戶:

import socket
import time
import sys

if len(sys.argv) > 1:
    maxSendRateBytesPerSecond = (int(sys.argv[1])*1024)
else:
    maxSendRateBytesPerSecond = (100*1024)

run_time = 60
total_packets = (maxSendRateBytesPerSecond/1024) * run_time

def ConvertSecondsToBytes(numSeconds):
   return numSeconds*maxSendRateBytesPerSecond

def ConvertBytesToSeconds(numBytes):
   return float(numBytes)/maxSendRateBytesPerSecond

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(('10.20.30.1', 6789))

# We'll add to this tally as we send() bytes, and subtract from
# at the schedule specified by (maxSendRateBytesPerSecond)
bytesAheadOfSchedule = 0

# Dummy data buffer, just for testing
dataBuf = bytearray(1024)

prevTime = None
t_end = time.time() + run_time
print("Sending %s Kbps for %s seconds (%s packets in total)" % (maxSendRateBytesPerSecond * 8, run_time, int(total_packets)))
while time.time() < t_end:
   now = time.time()
   if (prevTime != None):
      bytesAheadOfSchedule -= ConvertSecondsToBytes(now-prevTime)
   prevTime = now

   numBytesSent = sock.send(dataBuf)
   if (numBytesSent > 0):
      bytesAheadOfSchedule += numBytesSent
      if (bytesAheadOfSchedule > 0):
         time.sleep(ConvertBytesToSeconds(bytesAheadOfSchedule))
   else:
      print ("Error sending data, exiting!")
      break
end_string = b'END!'
sock.send(end_string)

服務器:

#!/usr/bin/python3

import socket
from datetime import datetime

UDP_IP_ADDRESS = "10.20.30.1"
UDP_PORT_NO = 6789

serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))
PACKET_COUNT = 0
while True:
        dt = datetime.now()
        data, addr = serverSock.recvfrom(1024)
        PACKET_COUNT = PACKET_COUNT + 1
        if data == b'END!':
                print ("Received " + str(PACKET_COUNT - 1))
                break

通過對每個數據包的有效載荷(1、2、3...)進行編號來實現序列檢查,在收到數字時保存它們,然后檢查序列,計算存在多少唯一序列

客戶:

import socket
import time
import sys

if len(sys.argv) > 1:
    maxSendRateBytesPerSecond = int(sys.argv[1])*128
if len(sys.argv) > 2:
    maxSendRateBytesPerSecond = int(sys.argv[1])*128
    run_time = int(sys.argv[2])
else:
    maxSendRateBytesPerSecond = (100*1472)
    run_time = 5
#Hard stop the app if it attempts to send traffic above 100mbit/s
if maxSendRateBytesPerSecond > 99000000:
    sys.exit("Bandwidth above 99Mbps is not allowed")
#Hard limit maximum running time to 5 minutes.
if run_time > 300:
    run_time = 300

#Total packets to be sent which is used for detecting packet loss.
total_packets = (maxSendRateBytesPerSecond/1472) * run_time

def ConvertSecondsToBytes(numSeconds):
    return numSeconds*maxSendRateBytesPerSecond

def ConvertBytesToSeconds(numBytes):
    return float(numBytes)/maxSendRateBytesPerSecond

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(('10.20.30.1', 6789))

# We'll add to this tally as we send() bytes, and subtract from
# at the schedule specified by (maxSendRateBytesPerSecond)
bytesAheadOfSchedule = 0

#Counter for packet sequencing.
counter = 0
prevTime = None
#Timer to break the loop.
t_end = time.time() + run_time
print("Sending %s Kbps for %s seconds (%s packets in total)" % (maxSendRateBytesPerSecond * 8, run_time, int(total_packets)))
while time.time() < t_end:
    now = time.time()
    #Create a DataBuf with the counter for integrity in bytes. Frame size is always 1472 bytes.
    dataBuf = counter.to_bytes(1472, 'big')
    #We calculate amount of time the script needed to send the data to the socket.
    if (prevTime != None):
        bytesAheadOfSchedule -= ConvertSecondsToBytes(now-prevTime)
    prevTime = now
    numBytesSent = sock.send(dataBuf)
    if (numBytesSent > 0):
        bytesAheadOfSchedule += numBytesSent
        if (bytesAheadOfSchedule > 0):
            time.sleep(ConvertBytesToSeconds(bytesAheadOfSchedule))
    else:
      print( "Error sending data, exiting!")
      break
    counter += 1
print(int.from_bytes(dataBuf, byteorder='big'))
end_string = b'END!'
sock.send(end_string)

服務器:

import socket
from datetime import datetime

UDP_IP_ADDRESS = "10.20.30.1"
UDP_PORT_NO = 6789

serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))
PACKET_COUNT = 0
sequence_list = []
while True:
        dt = datetime.now()
        data, addr = serverSock.recvfrom(1472)

        if data == b'END!':
            print ("Received " + str(PACKET_COUNT - 1))
            break
        else:
            sequence_list.append(int.from_bytes(data, byteorder='big'))
        PACKET_COUNT = PACKET_COUNT + 1

unique_sequences = []
count = 0

for i in range(len(sequence_list)-1):
    if sequence_list[i] + 1 == sequence_list[i+1]:
        if unique_sequences == []:
            unique_sequences.append([sequence_list[i]])
            count = count + 1
        elif sequence_list[i] != unique_sequences[-1][-1]:
            unique_sequences.append([sequence_list[i]])
            count = count + 1
        if sequence_list[i+1] != unique_sequences[-1]:
            unique_sequences[-1].extend([sequence_list[i+1]])
print("Out of order packets:", (len(unique_sequences)-1))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM