这些是我测试TCP套接字的基本Python程序。
server.py:
import socketserver as serv
class Handler(serv.BaseRequestHandler):
def handle(self):
received = self.request.recv(16)
print('received',received)
if __name__ == '__main__':
host, port = 'localhost', 2000
with serv.TCPServer((host,port), Handler) as serv:
serv.allow_reuse_address = True
serv.serve_forever()
client.py:
import socket as Socket
import time
if __name__ == "__main__":
host, port = 'localhost', 2000
with Socket.socket(Socket.AF_INET, Socket.SOCK_STREAM) as socket:
socket.connect((host, port))
socket.send(bytes('1','utf-8')) # First send
time.sleep(0.5)
socket.send(bytes('2','utf-8')) # Second send
首先运行服务器,然后运行客户端。服务器打印:
received b'1'
但是我想说这表明服务器收到了两个发送:
received b'1'
received b'2'
我错过了每次发送之间所需的内容吗?