Reputation: 13
Everything in the socket module is working, but socket.get
host by name gives a undesired error.
This is the client side code:
import socket
s = socket.socket()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname("DESKTOP-HC36K46")
port = 1122
s.connect((host,port))
print("CONNECTED TO SERVER")
while True:
data = s.recv(2)
print(str(data))
data = str(data)
if(data == "b'w'"):
print("W")
elif(data == "b's'"):
print("S")
elif(data == "b'a'"):
print("A")
elif(data == "b'd'"):
print("D")
elif(data == "b'wa'"):
print("WA")
elif(data == "b'wd'"):
print("WD")
elif(data == "b'sa'"):
print("SA")
elif(data == "b'sd'"):
print("SD")
This code does run well on a Windows PC, but when I try to run it on Linux(Raspbian or Kali Linux), it gives me the error
socket.gaierror: [Error -2] Name or Service not known
What can I do to resolve this problem?
Upvotes: 1
Views: 10223
Reputation: 222852
Your problem is socket.gethostbyname("DESKTOP-HC36K46")
- this should return the ip address associated with that name, but python doesn't do it by itself, all python does is to ask the OS for the address. Ultimately it is the system that will resolve the name to a ip address using its configured method, not python.
Your windows system is returning the correct address, but your linux system is probably misconfigured, and is not returning the address in this case.
In any case, this is not a python problem, and there is nothing to do, python side, to solve it, except maybe removing this line and hard-coding the ip address directly:
host = '192.168.0.4' # or whatever is the ip address
Upvotes: 1