Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 服务器和客户端问题:socket.gaierror:[Erno 11001]getaddrinfo失败_Python_Python 3.x_Networking_Network Programming - Fatal编程技术网

Python 服务器和客户端问题:socket.gaierror:[Erno 11001]getaddrinfo失败

Python 服务器和客户端问题:socket.gaierror:[Erno 11001]getaddrinfo失败,python,python-3.x,networking,network-programming,Python,Python 3.x,Networking,Network Programming,我对网络是如何工作的还不熟悉,我正在尝试为一个类分配编写两个脚本,一个作为服务器,一个作为客户端。服务器和客户端脚本都将在同一台计算机上运行,并且每个脚本使用两个不同的端口(客户端端口和服务器端口)。客户机应该能够向服务器发送消息,然后在发送成功时返回消息。这是客户端代码: from socket import * serverName = 'hostname' serverPort = 2000 clientSocket = socket(AF_INET, SOCK_DGRAM) messag

我对网络是如何工作的还不熟悉,我正在尝试为一个类分配编写两个脚本,一个作为服务器,一个作为客户端。服务器和客户端脚本都将在同一台计算机上运行,并且每个脚本使用两个不同的端口(客户端端口和服务器端口)。客户机应该能够向服务器发送消息,然后在发送成功时返回消息。这是客户端代码:

from socket import *
serverName = 'hostname'
serverPort = 2000
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = input('Input lowercase sentence:')
clientSocket.sendto(message.encode(), (serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(3000)
print(modifiedMessage.decode())
clientSocket.close()
这是服务器代码:

from socket import *
serverPort = 2000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print("The server is ready to receive")
while True:
   message, clientAddress = serverSocket.recvfrom(3000)
   modifiedMessage = message.decode().upper()
   serverSocket.sendto(modifiedMessage.encode(), clientAddress)
服务器代码运行正常,但运行客户机代码时出现以下错误:

socket.gaierror: [Erno 11001] getaddrinfo failed
具体来说,它不喜欢

clientSocket.sendto(message.encode(), (serverName, serverPort)

我在这里看到了关于这个错误的多个线程,但没有一个真正帮助我解决这个问题。在执行这两个脚本之前,我已经检查了确保这两个端口都已打开,并且它们都已打开。我最初的猜测是,它找不到实际的服务器端口,即使它正在运行并等待响应。所以我被难住了。此错误是什么意思?如何解决此问题?

在尝试发送消息之前,您忘记实际将客户端套接字连接到服务器套接字:

from socket import *
serverName = 'localhost'
serverPort = 2000
clientSocket = socket(AF_INET, SOCK_DGRAM)
clientSocket.connect((serverName, serverPort))
message = input('Input lowercase sentence:')
clientSocket.sendto(message.encode(), (serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(3000)
print(modifiedMessage.decode())
clientSocket.close()

我还将服务器名更改为
localhost

是主机名解析:可以使用
ping
?不需要连接
UDP
套接字。@Torxed刚刚注意到,似乎无法访问
hostname
。尽管如此,使用“localhost”仍然可以获得相同的结果。@Ernxst哇,这些都是小事,非常感谢!