UDP通信(python)
client:
import
socket
host =
‘127.0.0.1‘
port =
1024
buff_size =
128
addr = (host,port)
udp_client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while
True:
data = raw_input(‘>‘)
if
not data:
break
udp_client.sendto(data, addr)
udp_client.close()
server:
import
socket
host =
‘‘
port =
1024
buf_size =
128
addr = (host,port)
udp_server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_server.bind(addr)
while
True:
print
"wating for message..."
data,addr = udp_server.recvfrom(buf_size)
print
‘...received from and return to:‘
+ str(addr) +
": " + data
udp_server.close()
时间: 2024-11-06 01:06:52