import socket messages = ['This is the message ', 'It will be sent ', 'in parts ', ] server_address = ('localhost', 8090) # Create aTCP/IP socket socks = [socket.socket(socket.AF_INET, socket.SOCK_STREAM), socket.socket(socket.AF_INET, socket.SOCK_STREAM), ] # Connect thesocket to the port where the server is listening print('connecting to %s port %s' % server_address) # 连接到服务器 for s in socks: s.connect(server_address) for index, message in enumerate(messages): # Send messages on both sockets for s in socks: print('%s: sending "%s"' % (s.getsockname(), message + str(index))) send_data = message + str(index) s.sendall(bytes(send_data, encoding='utf-8')) # Read responses on both sockets for s in socks: data = s.recv(1024) print('%s: received "%s"' % (s.getsockname(), data)) if data != "": print('closingsocket', s.getsockname()) s.close()
从结果可以看到, 客户端会发送空字符过来, 查了下资料, 这是客户端在确定是否断开, 服务器端加个判断就行。
运行截图: