5
我想實現一個停止和等待算法。我在發件人實現超時時遇到問題。在等待來自reciever的ACK時,我正在使用recvfrom()函數。然而,這使程序空閒,我不能按照超時重新發送。停止和等待算法的Python實現
這裏是我的代碼:
import socket
import time
mysocket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
ACK= " "
userIn=raw_input()
if not userIn : break
mysocket.sendto(userIn, ('127.0.0.01', 88))
ACK, address = mysocket.recvfrom(1024) #the prog. is idle waiting for ACK
future=time.time()+0.5
while True:
if time.time() > future:
mysocket.sendto(userIn, ('127.0.0.01', 88))
future=time.time()+0.5
if (ACK!=" "):
print ACK
break
mysocket.close()
你真的不應該用一個空except子句,除非你重新拋出異常。你知道這將是一個socket.timeout,爲什麼不抓住那個? – drxzcl 2013-04-09 19:29:42
@drxzcl剛剛添加了那個;) – cmd 2013-04-09 19:34:27
'雖然沒有確認'而不是'確認'或我錯過了什麼? – mtahmed 2013-12-16 04:53:38