您的缩进似乎已在Python服务器中关闭,因为无法将消息发送回客户端的代码。
即使修复了缩进之后,您的服务器实现也不正确,而
msg不是
String。您需要
msg按如下所示进行解码。另外,
short由于要
DataInputStream#readUTF在客户端中使用,因此您需要发送消息的长度作为:
import socketsoc = socket.socket()host = "localhost"port = 2004soc.bind((host, port))soc.listen(5)while True: conn, addr = soc.accept() print("Got connection from",addr) length_of_message = int.from_bytes(conn.recv(2), byteorder='big') msg = conn.recv(length_of_message).depre("UTF-8") print(msg) print(length_of_message) # Note the corrected indentation below if "Hello"in msg: message_to_send = "bye".enpre("UTF-8") conn.send(len(message_to_send).to_bytes(2, byteorder='big')) conn.send(message_to_send) else: print("no message")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)