我正在使用python3代码在一个响应器3.这是在服务器上的代码:
import json, time, requests, signal, sys, socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
sock.bind((s.settings["udpIP"], s.settings["udpPort"]))
sessions = {}
c = 0
while True:
data, addr = sock.recvfrom(65535)
if somedata is x:
sequence = <clientSessionSeq> #startint at 000 and then 001 ( to monitor the udp packet so i wont lose any packets)
sock.sendto(sequence, (addr)) # returning the client the seq i got so it may proceed sending the next one, if its not the same seq client will resend lost package
s.print_info(
{
"REMOTE_ADDRESS": addr,
"STATUS": "PUBLISH",
},
)
c = 0
c = c + 1客户端代码-
data = data_iter()
while True:
# if seq is ok then pull next data
dataTmp = next(data)
sock.sendto(
"ID ".encode() + headers["SESSION_ID"].encode() + sequence + dataTmp,
(s.settings["server"]["ip"], s.settings["udpPort"]),
)
try:
sock.settimeout(3)
response, addr = sock.recvfrom(1024)
except IOError as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback)
s.print_info(e)我的问题是在response, addr = sock.recvfrom(1024)的客户端代码中,我有时会在3秒后获得超时,服务器向客户端发送它收到的seq,但客户端recvfrom被卡住\延迟。
有没有办法在raspberrypi或套接字代码中对udp进行一些调整?
我不能使用tcp,我知道它会工作得更好,我必须使用udp
如果我删除recivefrom,它运行良好,但我有数据包丢失,我使用这个流记录,所以我必须得到完整的记录。我只需要调优的建议,或者可能是使用udp而不是socket的另一种方式?
谢谢。
发布于 2019-12-12 04:28:24
这可能不是由于UDP“非常慢”造成的,实际上只是丢包的一个例子。既然你说你需要在UDP中工作,你需要自己处理这个问题。
我假设您最终将不得不编写与TCP中存在的代码非常相似的代码,所以我建议您查看一下以前是如何解决的。TCP使用一种称为滑动窗口的东西来跟踪任何时候“飞行”的数据包的状态,并且应该有很多现有的文档来描述它们如何工作/为什么工作。
https://stackoverflow.com/questions/59255054
复制相似问题