1

I need help with sockets in python. A device (D) is directly connected via an Ethernet cable to an USB-to-Ehternet adpater (eth1) plugged into a Raspberry Pi. ifconfig and running sudo tcpdump -i eth1 show that D is communicating under IP address 169.254.129.33. When I run following code in Python, I don't receive a reply from D, although the message sent is said to trigger a response.

import socket

HOST = socket.gethostbyname(socket.gethostname())
PORT = 30444
D_IP = '169.254.129.33'

msg = 'Calling all IRC devices'

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST, PORT))
    s.sendto(msg.encode(), (D_IP, PORT))
    data, address = s.recvfrom(PORT)
    s.close()

print(address, ' replied: ', data.decode())

It seems like Python is either not sending msg via the adapter to D or D's IP address is not the one I thought it was.

How can I tell python to specifically use the USB-to-Ethernet adapter for UDP communication? How do I get the correct IP address of D when attached to the Pi and not to a router?

P.S.: Hope, the formatting is okay and my English isn't too bad.

PhyAJ
  • 11
  • 2
  • If you do a `ping 169.254.129.33` from that same machine, does ping report getting a response back? – Jeremy Friesner Jan 20 '20 at 15:43
  • @JeremyFriesner Ping reports 0% packet loss, however, `sudo tcpdump -i eth1` doesn't show anything going out on that port... – PhyAJ Jan 20 '20 at 16:06
  • Perhaps `169.254.129.33` is the IP address of the machine that ping is running on, rather than the remote machine? – Jeremy Friesner Jan 20 '20 at 17:18
  • @JeremyFriesner But according to Wikipedia at Link-local address, it should be a link-local address at least. Also wireshark detects incoming requests from that address when connecting it to the adapter... Meanwhile I tried adding SO_BINDTODEVICE (as asked here [https://stackoverflow.com/questions/7221577/how-to-bind-socket-to-an-interface-in-python-socket-so-bindtodevice-missing] ) but that doesn't work either. – PhyAJ Jan 20 '20 at 17:24
  • Does the Pi have a link-local address on eth1? That's the address that your program needs to `bind` to, but it's almost certainly not the result that you'll get from `socket.gethostbyname(socket.gethostname())`. Even easier, do not `bind` to an address at all, just do `bind("", PORT)`. – ottomeister Jan 20 '20 at 23:27
  • Also, the argument to `recvfrom` should be a buffer size, not a port number. In this case it'll probably work by accident because `PORT` is a large-ish number, but it's still a bug. – ottomeister Jan 20 '20 at 23:30
  • @ottomeister Okay, I tried `bind("", PORT)` and also had the program print the host's IP address (`socket.gethostbyname(socket.gethostname())`) which showed that `169.254.129.33` is indeed the Pi's local-link address as @JeremyFriesner suggested. How do I find the correct IP address of the attached device? Also, thanks for the information on `recvfrom`. – PhyAJ Jan 21 '20 at 07:12

0 Answers0