0

I have a raspberry pi 4 with RaspberryOS. I am doing a few tests with socket programming, and I used to able to connect using hostname 'raspberry' only, with which I could get the ip address of the pi, which is not static. However, after changing the wifi router, it stopped working and I couldn't get ip address from hostname anymore. It doesn't even get the ip address of the pi itself.

serverscript.py, on the pi:

#!/usr/bin/python3
import time
import threading
import socket

#host = socket.gethostbyname(socket.gethostname())
host = socket.gethostbyname('raspberry')
port = 1023

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
    try:
        serversocket.bind((host, port))
        break
    except:
        port += 1
print((host, port))

serversocket.listen(5)
connection, address = serversocket.accept()

print("Got a connection")

piservertest.py on a laptop trying to connect to pi:

import socket

host = socket.gethostbyname('raspberry')
port = 1023
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
    try:
        print(port)
        sock.connect((host, port))
        break
    except:
        port+=1

while True:
    pass

The server script socket keeps binding to 127.0.1.1, while the client script keeps giving [Errno 11004] getaddrinfo failed. I am new to both pi and socket programming, any help would be appreciated on how to fix this.

Edit: Ping requests from my laptop cannot find the pi either. ping -4 raspberry shows the following:

enter image description here

/etc/hosts after commenting out the part as suggested by @Bodo:

enter image description here

pinging raspberry from the pi itself now shows the same error as on the laptop.

5
  • So the basic problem is the name resolution or assignment in combination with a changed router. Can you reproduce the problem without your program by using ping raspberry? Are you sure the name is raspberry and not raspberrypi?
    – Bodo
    Nov 10 at 16:36
  • Ping requests can't find the pi either, and I'm pretty sure it shows up as raspberry in glasswire and other software. Nov 10 at 17:19
  • If ping reproduces the problem, then your code distracts from the actual problem of name resolution. Showing the result of ping -4 raspberry on both systems should be sufficient. Please edit your question and add all information there instead of using comments. I guess on your Pi you have a line 127.0.0.1 raspberry in /etc/hosts which I think is wrong. Please show the contents of this file. 127.0.0.1 allows looopback connections only. If you comment out this line using # you will probably get a similar error on yout Pi. (I don't know how glasswire finds out the host name.)
    – Bodo
    Nov 11 at 15:35
  • Added the edits. Nov 12 at 16:25
  • Please don't show images of text like file contents or command output. Copy&paste it as text, formatted as code blocks, instead. The change shows that your Raspberry Pi doesn't seem to get a host name assigned in your router's DNS. You wrote "However, after changing the wifi router, it stopped working". This must be the cause of the problem. It is not related to your Raspberry pi but to your router or network. Without knowing any details about what you changed we cannot suggest a solution.
    – Bodo
    2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.