[英]ConnectionRefusedError: [Errno 111] Connection refused between laptop and raspberry pi using python
I am a beginner in socket communication and I wanted to test this code.我是套接字通信的初学者,我想测试这段代码。
I wrote the same code but changed the host in the server to s.gethostname() when both the client and server were on my laptop and worked normally.我编写了相同的代码,但是当客户端和服务器都在我的笔记本电脑上并且正常工作时,将服务器中的主机更改为 s.gethostname()。
server: Laptop服务器:笔记本电脑
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 62402
s.bind((host,port))
s.listen(5)
while True:
clientsocket, address = s.accept()
print(f"connection from {address} has been established!")
clientsocket.send(bytes("Welcome to the server!","utf-8"))
client: raspberry pi客户:树莓派
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 62402
s.connect((host,port))
msg = s.recv(1024)
print(msg.decode('utf-8')
Error错误
Traceback(most recent call last):
File "/home/pi/Desktop/testting/client.py", line 6, in <module>
s.connect((host,port))
ConnectionRefusedError: [Error 111] Connection refused
Connection refused tells me that [the target] is not accepting the connection.连接被拒绝告诉我 [目标] 不接受连接。 I don't think ´socket.gethostname()´ can possibly return the laptop's hostname in its current form.
我认为“socket.gethostname()”不可能以当前形式返回笔记本电脑的主机名。 ´print()´ what it returns - I'd bet it's the local machine's hostname (the one creating the connection).
'print()' 它返回什么 - 我敢打赌它是本地机器的主机名(创建连接的主机名)。
Once you know where you're connecting to, Things that could go wrong:一旦你知道你要连接到哪里,可能会出错的事情:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.