python:udp-socket仅在线程中发送-接收一次
但是我可以使用udp-socket在没有线程的循环中发送多个请求
class mydo(threading.Thread):
def __init__(self,count):
super(mydo, self).__init__()
self.count = count
def run(self):
while self.count > 0:
self.count -= 1
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
request = json.dumps({"Request": ["setMessage", "Hello !!"]})
if 0 == self.count:
request = json.dumps({"Request": ["setMessage", "Hello @@@"]})
sock.sendto(request, ('127.0.0.1', 8080))
tmp = sock.recvfrom(1024)
sock.close()
numOfThreads = 1
threads = []
count = 50
for i in range(numOfThreads):
t = mydo(count)
threads.append(t)
for t in threads:
t.start()
在线程中,套接字仅发送最后一个请求(预期为50),服务器仅接收最后一个请求。