简体   繁体   English

套接字错误:无法建立连接,因为目标机器主动拒绝它

[英]Socket Error : No connection could be made because targeted machine actively refused it

Note : This problem has been completely solved, as was am running client.py before server.py注意:这个问题已经完全解决了,因为我在 server.py 之前运行 client.py

Just got started with socket programming, I have created the below code and expecting to print some byte message, but it isn't doing that.刚刚开始使用套接字编程,我创建了以下代码并希望打印一些字节消息,但它并没有这样做。

I just want to make the message available for any person on any machine.我只想让任何机器上的任何人都可以使用该消息。 But it's refusing by the machine to do that.但是机器拒绝这样做。

Here is my code:这是我的代码:

server.py服务器.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 12048

s.bind((socket.gethostname(), port))

s.listen()

while True:
    c, addr = s.accept()
    print("Got connection from", addr)

    c.send(bytes("Thank you", "utf-8"))

client.py客户端.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 12048

s.connect(('192.168.0.1', port))

msg = s.recv(1024)
print(msg.decode("utf-8"))

Some images to better explain my errors:一些图片可以更好地解释我的错误:

服务器.py

客户端.py

Any help would be appreciated!!!任何帮助,将不胜感激!!!

It looks like in the server.py script you use s.bind((socket.gethostname(), port)) where socket.gethostname() is a hostname, but in the client.py script you use s.connect(('192.168.0.1', port)) where '192.168.0.1' is the hostname you are trying to connect.看起来在server.py脚本中您使用s.bind((socket.gethostname(), port))其中socket.gethostname()是主机名,但在client.py脚本中您使用s.connect(('192.168.0.1', port))其中'192.168.0.1'是您尝试连接的主机名。

I think there you have socket.gethostname() != '192.168.0.1' and that's the problem.我认为你有socket.gethostname() != '192.168.0.1'这就是问题所在。

Also, you can bind to all available IP addresses on the host using this solution Python socket bind to any IP?此外,您可以使用此解决方案绑定到主机上的所有可用 IP 地址Python socket bind to any IP?

Let's use listen_ip = socket.gethostbyname(socket.gethostname()) for connection since socket.gethostname() may return hostname instead of IP and it will not be solved by python dns resolver in local network if it was local name, not DNS.让我们使用listen_ip = socket.gethostbyname(socket.gethostname())进行连接,因为socket.gethostname()可能返回主机名而不是 IP,并且如果它是本地名称,而不是 DNS,它不会被本地网络中的 python dns 解析器解析。

and use it later as s.bind((listen_ip, port)) and s.connect((listen_ip, port))稍后将其用作s.bind((listen_ip, port))s.connect((listen_ip, port))

After some debugging I've got a working solution for you经过一些调试后,我为您提供了一个可行的解决方案

There are the scripts required.有所需的脚本。

server.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
port = 12_048

s.bind((host, port))

s.listen()
print("Server listening @ {}:{}".format(host, port))

while True:
    c, addr = s.accept()
    print("Got connection from", addr)

    c.send(bytes("Thank you", "utf-8"))

client.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '192.168.1.162'  # The IP printed by the server must be set here

# or we can set it using env variable named SERVER_IP
if 'SERVER_IP' in os.environ:
    host = os.environ['SERVER_IP']

port = 12048

print("Connecting to {}:{}".format(host, port))
s.connect((host, port))

msg = s.recv(1024)
print(msg.decode("utf-8"))

In a chat conversation we concluded that the hardcoded IP in the question is not the correct one.在聊天对话中,我们得出结论,问题中的硬编码 IP 不正确。 This solution does have the IP he needed but it will be different in each case.此解决方案确实具有他需要的 IP,但在每种情况下都会有所不同。 Remeber that server.py needs to be launched first, and when you see the printed Server listening @ IP:12048 , write that IP in client.py and launch it.请记住,需要先启动server.py ,当您看到打印的Server listening @ IP:12048 ,将该 IP 写入client.py并启动它。 Client does need to be launched after seeing that line even if you already know the IP, as the server needs some time to be ready and the client will crash if it tries to connect to the server while it is not ready.即使您已经知道 IP,也确实需要在看到该行后启动客户端,因为服务器需要一些时间来准备好,如果客户端在未准备好时尝试连接到服务器,则客户端会崩溃。

server.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
port = 12048

s.bind((host, port))

s.listen()
print("Server listening @ {}:{}".format(host, port))

while True:
    c, addr = s.accept()
    print("Got connection from", addr)

    c.send(bytes("Thank you", "utf-8"))

client.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '192.168.1.162'  # The IP printed by the server must be set here
port = 12048

print("Connecting to {}:{}".format(host, port))
s.connect((host, port))

msg = s.recv(1024)
print(msg.decode("utf-8"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 中 TCP 连接的简单套接字编程练习; 错误:“无法建立连接,因为目标机器主动拒绝它” - simple socket programming exercise for TCP connections in Python; Error: "No connection could be made because the target machine actively refused it" 发送套接字时出错[WinError 10061]无法建立连接,因为目标计算机主动拒绝将Socket从Python发送到Android - Error sending socket [WinError 10061]No connection could be made because the target machine actively refused it for Socket from Python to Android Python Socket 编程 - ConnectionRefusedError: [WinError 10061] 无法建立连接,因为目标机器主动拒绝了它 - Python Socket Programming - ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it Python-无法建立连接,因为目标计算机主动拒绝了它 - Python - No connection could be made because the target machine actively refused it Python:无法建立连接,因为目标机器主动拒绝它 - Python: No connection could be made because the target machine actively refused it WinError 10061 由于目标机器主动拒绝,无法建立连接 - WinError 10061 No connection could be made because the target machine actively refused it “[WinError 10061] 无法建立连接,因为目标机器主动拒绝它” - "[WinError 10061] No connection could be made because the target machine actively refused it" EmailMultiAlternatives无法建立连接,因为目标计算机主动拒绝了它 - EmailMultiAlternatives no connection could be made because the target machine actively refused it 无法建立连接,因为目标计算机主动拒绝它 - No connection could be made because the target machine actively refused it Django Python - 无法建立连接,因为目标机器主动拒绝它 - Django Python - No connection could be made because the target machine actively refused it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM