这是我想出的解决办法……以防万一有一天,有人在类似的情况下可能需要帮助。
其思想是将所有行打包在一条消息中并将其发送回客户端。
它的工作原理似乎是,对于客户机发出的每个请求,服务器只需要做出一个回复,而只需要一个回复。至少我是这么看的。。
替换服务器端的代码# Send reply back to client
with open("test.txt", 'r') as f:
for line in f:
socket.send_string(line.rstrip("\n"))
使用:# Send reply back to client
with open("test.txt", 'r') as f:
message = '%s' % f.readlines()
print(message)
print(type(message))
socket.send_string(message)
客户请求Connecting to hello world server
Sending request 0
Received reply 0 [ ['This is test line 1\n', 'This is test line 2\n', 'This is test line 3\n', 'This is test line 4\n', 'This is test line 5\n', 'This is test line 6\n', 'This is test line 7\n', 'This is test line 8\n', 'This is test line 9\n', 'This is test line 10\n', '\n'] ]
Sending request 1
Received reply 1 [ ['This is test line 1\n', 'This is test line 2\n', 'This is test line 3\n', 'This is test line 4\n', 'This is test line 5\n', 'This is test line 6\n', 'This is test line 7\n', 'This is test line 8\n', 'This is test line 9\n', 'This is test line 10\n', '\n'] ]
....
....
and so on up to 10 requests
服务器响应Received request: Hello
['This is test line 1\n', 'This is test line 2\n', 'This is test line 3\n', 'This is test line 4\n', 'This is test line 5\n', 'This is test line 6\n', 'This is test line 7\n', 'This is test line 8\n', 'This is test line 9\n', 'This is test line 10\n', '\n']
Received request: Hello
['This is test line 1\n', 'This is test line 2\n', 'This is test line 3\n', 'This is test line 4\n', 'This is test line 5\n', 'This is test line 6\n', 'This is test line 7\n', 'This is test line 8\n', 'This is test line 9\n', 'This is test line 10\n', '\n']
....
....
....
and so on....
现在已经解决了这个问题,接下来的问题是:客户端需要发送什么样的请求才能以逐行方式接受服务器的响应。
如果我有解决方案,或者你可以自由参与,我会更新回复。