UDP套接字客户端不接收来自服务器的消息

时间:2018-02-17 19:52:03

标签: python sockets network-programming

这是python的服务器代码

from socket import *


serverPort = 12000

serverSocket = socket(AF_INET, SOCK_DGRAM)

serverSocket.bind(('', 12000))

print(" the server is ready to receive")

while 1:
     message, clientAddress = serverSocket.recvfrom(2048)
     modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)

,客户端代码是

from socket import * 
serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = input ('Input lowercase sentense:')
clientSocket.sendto(message.encode(),(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print (modifiedMessage)
clientSocket.close()

我首先运行服务器,输出是

enter image description here

然后我运行客户端并输入小写句子,之后客户端和服务器不做任何其他事情

enter image description here

请帮帮我。

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

while 1:
    message, clientAddress = serverSocket.recvfrom(2048)
    modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)`

Python中的缩进非常重要。您的服务器进程永远不会到达第三个语句,因为它在无限循环结束后在技术上定位。