2015-11-12 85 views
1

這是問題是真正關注我的問題,而不是相對於我可以找到關於此主題的任何其他問題。丟棄數據報套接字的傳入'數據包

PSA:當我說「包」我的意思是在一個單一的socket.recv(MAXSIZE)接收到一個完整的字符串

我開發了類似的代碼Java的(我PREF語言)和它相同的結果是好的,現在我必須做python。

我有兩個並行運行的進程: 1-常規客戶端套接字連接到特定IP 2-A「客戶端」數據報套接字綁定到「ALL」IP。

正常的套接字工作正常,因爲我期望,而數據報沒有。

我連續從服務器(不是我的,不是開源)接收數據包的速度超過每秒5次,但我想每3秒只處理其中的一個。在java中,我只是做了一個「睡眠」,並沒有問題,我只得到最後一個活動數據包,而在Python中使用「time.sleep(3)」數據包排隊(我不知道如何以及在哪裏)並沒有下降。

我必須放棄它們,因爲這些都不需要,我必須在一個和另一個之間進行HTTP調用,所以我無法爲以該速率接收的每組數據啓動HTTP POST!

這裏是我的「代碼」爲監聽套接字,一些意見是對私有代碼:

def listenPositions(): 
    lsSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    lsSocket.bind(("0.0.0.0", 8787)) 
    lsSocket.setblocking(0) 
    try: 
     while True: 
      ready = select.select([lsSocket], [], [], 1) 
      if ready[0]: 
       lsSocket.settimeout(1) 
       recvData = lsSocket.recv(16384) 
       if len(recvData) != 0: 
        recv = recvData[0:len(recvData)].decode("utf-8") 
        #print("LS: Received: " + recv) 
        strings = filter(None, str(recv).split('\n')) 
        print("Strings count=" + str(len(strings))+ ": " + str(strings)) 
        for item in strings: 
         #parse the received strings as json and get the items 
         jsonPosition = json.loads(item) 
         strId = jsonPosition["id"] 
         coordinates = jsonPosition.get("coordinates") 
         if coordinates is None: 
          continue 
         print("coordinates not null:" + str(coordinates)) 
#DO THE HTTP POST REQUEST 


        time.sleep(3) #Pause the system for X seconds, but other packets are queued! 
       else: 
        print("LS: Received empty") 
      else: 
       print("LS: No data, timeout") 
    except Exception as e: 
     print(e) 
     #handle exceptions... 
     print("Exception, close everything") 

回答

1

當你有一個開放的插座,所有正確處理數據包應該交付給應用程序。我們希望儘可能使我們的網絡連接更加可靠,是嗎?丟棄數據包是最後一招。

如果您只想不時收到一個數據包,您可以創建一個監聽套接字,獲取一個數據包並關閉套接字。

然而沒有什麼比忽略數據包更容易。只是跳過它的處理,繼續前進。下面的代碼是不完整的,但希望表達我的意思。

TIMEOUT = 1.0 
INT = 3.0  # interval in seconds 

# create udp_socket 
last = time.time() - INT 
udp_socket.settimeout(TIMEOUT) 
while True: 
    try: 
     packet = udp_socket.recv(MAXSIZE) 
    except socket.timeout: 
     # handle recv timeout 
     continue # or break, or return 
    except OSError: 
     # handle recv error (Python 3.3+) 
     break  # or continue, or return 
    now = time.time() 
    if now - last >= INT: 
     # process the packet 
     last = now 

請注意,如果您只能從一個源讀取,則不需要select