Farhan Kabir
Farhan Kabir

Reputation: 169

UDP Socket is not receiving any message with python

I am trying to send message from Raspberry Pi (Ubuntu 20) to Laptop (Virtualbox Ubuntu 20) via UDP socket. So I am using simple code from https://wiki.python.org/moin/UdpCommunication

Sending (from Raspberry Pi)

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = b"Hello, World!"

print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

Receiving (from laptop)

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

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: %s" % data)

I tried UDP_IP = "0.0.0.0" in both ends. I tried Ip address of my laptop at RPI end. I tried both machine's IP address at both ends. I tried sock.bind(("", UDP_PORT)) to bind all. I tried adding the UDP port number in firewall settings. I checked multiple questions from this forum related to this.

Still, I cannot get any packets at the laptop receiving side. I do not know what is wrong. Please advise.

Upvotes: 1

Views: 2388

Answers (3)

Make sure the port you are using have UDP enabled in windows 10.

To open any UDP ports, you can do the following:

  1. Go to Control Panel> System and Security and Windows Firewall.
  2. Advanced settings > right-click Inbound Rules and select New Rule.
  3. Add the port(s) you want to open and click Next.
  4. Select UDP protocol and the port(s) number(s) into the next window and click Next.
  5. Select Allow the connection and hit Next.
  6. Select the network type and click Next.
  7. Give a name for the rule and click Finish.

Upvotes: 0

Roberto Raskovsky
Roberto Raskovsky

Reputation: 1

If you are using a static IP on the RPi, you need to add a static route:

sudo ip route add 236.0.0.0/8 dev eth0

Upvotes: 0

Mateo Lara
Mateo Lara

Reputation: 917

The problem may be in the IP address because you are using the IP '127.0.0.1' (localhost) to reach an outside device. Please find out the IPs of your devices, try using ifconfig Linux command. Also, check that nothing is blocking your connection.

Consider that for socket.bind(address) you can use '0.0.0.0' and for socket.sendto(bytes, address) you should use the IP of the device you want to send to.

I recommend you to download a program called Hercules, with this program you can create a UDP peer to figure out what is not working properly. For example, you could use python in one side and Hercules in the other to rule out mistakes in one side of the code execution, you could also try two Hercules connections and see if you can establish communication in which case the problem is most likely related to the code execution, in the other hand if you can not establish a connection between the two Hercules UDP peers the problem is most likely with the devices or the network itself.

Upvotes: 2

Related Questions