python - Cannot send and receive udp message in same program -
i able send , receive udp messages in separate programs, i'm not able same task in 1 program.
import socket udp_ip = "192.168.1.178" udp_port = 8888 msg = 'test' print "udp target ip: ", udp_ip print "udp target port: ", udp_port print "message: ", msg sock = socket.socket(socket.af_inet, socket.sock_dgram) sock.sendto(msg, (udp_ip, udp_port)) udp_ip2 = "192.168.1.198" sock.bind((udp_ip2, udp_port)) while true: data, addr = sock.recvfrom(1024) # buffer size 1024 bytes print "received message:", data
with program, able send udp messages, however, not able receive messages other machine.
what doing wrong?
in advance,
mikkel
in example try bind socket addr after sending, what's wrong. address can bound socket before data transfer.
if there no explicit bind os sets free (unused) port number in range [1024, 65535] on first .send()/.recv()
call.
next, socket can bound single ip (except special case '0.0.0.0' means "all host's interfaces").
Comments
Post a Comment