python 中socket.error: [Errno 10022]

This error keeps appearing when using the socket module in python to change udp

The error code is posted below

server:

import socket
import random,time
s = socket.socket(socket.AF_INET , socket.SOCK_DGRAM)

host = '127.0.0.1'
port = 21567
addr = (host , port)

if __name__ == '__main__':
	s.bind(addr)
	while True:
		time.sleep(5)
		data = random.random()
		print('sendto data: %s'%str(data))
		s.sendto(str(data),addr)
	s.close()

client

	import socket
	s = socket.socket(socket.AF_INET , socket.SOCK_DGRAM)
	while True:
		data,addr = s.recvfrom(1024)
		q.put(data)
		time.sleep(random.random())


When this code is running, errno 10022 keeps appearing, and it took me an afternoon to find out 


the reason:

When a udp needs to accept data, first let the operating system know that the port where this program is located, that is, it needs to be bound

And I send data through the server, and the client receives the data. Therefore, the client does not know which port belongs to it.  

That's why this error keeps happening


Published 190 original articles · 19 praises · 200,000+ views

Guess you like

Origin blog.csdn.net/zengchenacmer/article/details/42424015