【发布时间】:2018-05-25 19:38:43
【问题描述】:
我正在使用 UDP 实现客户端-服务器模型。客户端向服务端发送数据,服务端除了接收之外,还需要对其进行解析。现在,如果我在收到数据后没有解析数据,服务器会将 100% 的数据写入文件。但是在解析时,它会延迟,并且一些数据不会写入输出文件。
服务器代码:
while True:
data, addr = s.recvfrom(1024)
#I used pickle since the data is an array
csv_writer.writerow(pickle.loads(data))
#Stating to parse the data now:
arr = pickle.loads(data) <---- The data I need to parse is an array
# If the key exists in the dictionary,
# increment the value by one
currentMessageID = arr[0]
if (dictOfMessageID.has_key(currentMessageID)):
dictOfMessageID[currentMessageID] += 1
# # If the key doesn't exists in dictionary
# # add the key-value pair
else:
dictOfMessageID[currentMessageID] = 1
我该如何解决这个问题?服务器仍然需要写入所有数据并解析它 或者我错过了什么,UDP 数据包会在这种情况下丢失?
【问题讨论】:
标签: python-2.7 sockets server buffer pickle