I'm facing an issue to send and receive message using UDP socket with Python.
The problem is when I try to send and receive a message using this code i don't receive anything:
Receiver.py:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 55681
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
Sender.py:
import socket
UDP_IP = "192.168.1.161"
UDP_PORT = 55681
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
But if I change the code of the Receiver by this :
import socket
UDP_IP = "192.168.1.161"
UDP_PORT = 55681
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
It works perfectly.
Also it is the same case if I put 127.0.0.1 as addresses in both programs.Why ?
I work on linux with python 2.7 and this is the result of ifconfig:
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:12252 errors:0 dropped:0 overruns:0 frame:0
TX packets:12252 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1528301 (1.5 MB) TX bytes:1528301 (1.5 MB)
wlan0 Link encap:Ethernet HWaddr 00:21:6a:5b:b4:dc
inet addr:192.168.1.161 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: XXXX::XXX:XXXX:XXXX:XXXX/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:532561 errors:0 dropped:0 overruns:0 frame:0
TX packets:318747 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:629969205 (629.9 MB) TX bytes:32669218 (32.6 MB)
Thanks by advance for your answers.