简体   繁体   English

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

[英]Server and client issue: socket.gaierror: [Erno 11001] getaddrinfo failed

I'm new to how networking works and I'm trying to write two scripts for a class assignment, one acts as a server and one as a client. 我是网络工作原理的新手,我正在尝试为类分配编写两个脚本,一个充当服务器,一个充当客户端。 Both the server and client scripts are to be run on the same computer and each one uses two different ports (client port and server port). 服务器和客户端脚本都将在同一台计算机上运行,​​并且每个脚本都使用两个不同的端口(客户端端口和服务器端口)。 The client should be able to send a message to the server and then get a message back if the send was successful. 客户端应该能够将消息发送到服务器,如果发送成功,则可以获取消息。 This is the client code: 这是客户端代码:

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()

And this is the server code: 这是服务器代码:

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)

The server code runs fine but I get this error from running the client code: 服务器代码运行正常,但是从运行客户端代码时出现此错误:

socket.gaierror: [Erno 11001] getaddrinfo failed

Specifically, it doesn't like 具体来说,它不喜欢

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

I saw multiple threads on here about this error but none of them really helped with my issue. 我在这里看到多个有关此错误的线程,但是它们都没有真正帮助解决我的问题。 I already checked to ensure both the ports are definitely open before executing both scripts, and they are. 在执行两个脚本之前,我已经检查确保两个端口都已打开,并且它们确实已经打开。 My initial guess is that it can't find the actual server port, even though it's up and running and waiting for a response. 我最初的猜测是,即使启动并正在运行并等待响应,它也无法找到实际的服务器端口。 So I'm stumped. 所以我很困惑。 What does this error mean and how can I resolve this? 此错误是什么意思,我该如何解决?

You forgot to actually connect the client socket to the server socket before trying to send a message: 您在尝试发送消息之前忘记将客户端套接字实际连接到服务器套接字:

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()

I also changed the serverName to localhost . 我也将serverName更改为localhost

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

相关问题 socket.gaierror:[Errno 11001] getaddrinfo 失败 - socket.gaierror: [Errno 11001] getaddrinfo failed (Python)socket.gaierror:[Errno 11001] getaddrinfo失败 - (Python) socket.gaierror: [Errno 11001] getaddrinfo failed Scapy导入错误'socket.gaierror:[Errno 11001] getaddrinfo失败' - Scapy import error 'socket.gaierror: [Errno 11001] getaddrinfo failed' 错误:- socket.gaierror:[Errno 11001] getaddrinfo 在获取请求中失败 - Error :- socket.gaierror: [Errno 11001] getaddrinfo failed in get request 如何解决:socket.gaierror: [Errno 11001] getaddrinfo failed - How to solve: socket.gaierror: [Errno 11001] getaddrinfo failed getaddrinfo失败,出现socket.gaierror [11001](python)(mqtt) - getaddrinfo failed with socket.gaierror[11001] (python) (mqtt) socket.gaierror: [Errno 11001] getaddrinfo 在 django 中失败 - socket.gaierror: [Errno 11001] getaddrinfo failed in django 服务器连接问题:“socket.gaierror: [Errno 11004] getaddrinfo failed” - Server connection issue: “socket.gaierror: [Errno 11004] getaddrinfo failed” gaierror socket.gaierror: [Errno 11001] getaddrinfo 失败 mail.send(msg) - gaierror socket.gaierror: [Errno 11001] getaddrinfo failed mail.send(msg) 当我在 python 中使用套接字模块时,我收到此错误:'socket.gaierror: [Errno 11001] getaddrinfo failed' - I get this error:' socket.gaierror: [Errno 11001] getaddrinfo failed' when I use the socket module in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM