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