我有一个客户端连接到服务器并发送列表中的所有消息,并且发送的每条消息都会从列表中删除。
但是当我强制关闭服务器时,客户端仍然继续从列表中发送和删除消息。我希望如果连接断开,客户端停止发送消息,或者如果不能保证服务器已收到消息,则不要从列表中删除。
我检查当我在连接断开后发送超过四条消息时,显示错误“socket.send() 引发异常”。但我不知道如何得到那个错误,我认为它是异步的。无论如何,如果我收到该错误,如果列表在连接断开后发送的消息少于五条,则不会发生该错误。
Ps:我写服务器只是为了我的测试,但是我将无法访问服务器。因此,需要尽一切努力保证数据发送的客户端。
太感谢了。
客户端.py
import asyncio
class Client(asyncio.Protocol):
TIMEOUT = 1.0
event_list = []
for i in range(10):
event_list.append('msg' + str(i))
def __init__(self):
self.client_tcp_timeout = None
print(self.event_list)
def connection_made(self, transport):
print('Connected to Server.')
self.transport = transport
self.client_tcp_timeout = loop.call_later(self.TIMEOUT, self.send_from_call_later)
def data_received(self, data):
self.data = format(data.decode())
print('data received: {}'.format(data.decode()))
def send_from_call_later(self):
self.msg = self.event_list[0].encode()
self.transport.write(self.msg)
print('data sent: {}'.format(self.msg))
print('Removing data: {}'.format(self.event_list[0]))
del self.event_list[0]
print(self.event_list)
print('-----------------------------------------')
if len(self.event_list) > 0:
self.client_tcp_timeout = loop.call_later(self.TIMEOUT, self.send_from_call_later)
else:
print('All list was sent to the server.')
def connection_lost(self, exc):
print('Connection lost!!!!!!.')
loop = asyncio.get_event_loop()
coro = loop.create_connection(Client, 'localhost', 8000)
client = loop.run_until_complete(coro)
loop.run_forever()
服务器.py
import asyncio
class Server(asyncio.Protocol):
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
print('connection from {}'.format(peername))
self.transport = transport
def data_received(self, data):
print('data received: {}'.format(data.decode()))
#self.transport.write(data)
loop = asyncio.get_event_loop()
coro = loop.create_server(Server, 'localhost', 8000)
server = loop.run_until_complete(coro)
print('serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
print("exit")
finally:
server.close()
loop.close()
服务器输出(在收到 msg4 后强制 (CTRL+C) 关闭服务器):
$ python3 server.py
serving on ('127.0.0.1', 8000)
connection from ('127.0.0.1', 56119)
data received: msg0
data received: msg1
data received: msg2
data received: msg3
data received: msg4
^Cexit
客户端输出
$ python3 client.py
['msg0', 'msg1', 'msg2', 'msg3', 'msg4', 'msg5', 'msg6', 'msg7', 'msg8', 'msg9']
Connected to Server.
data sent: b'msg0'
Removing data: msg0
['msg1', 'msg2', 'msg3', 'msg4', 'msg5', 'msg6', 'msg7', 'msg8', 'msg9']
-----------------------------------------
data sent: b'msg1'
Removing data: msg1
['msg2', 'msg3', 'msg4', 'msg5', 'msg6', 'msg7', 'msg8', 'msg9']
-----------------------------------------
data sent: b'msg2'
Removing data: msg2
['msg3', 'msg4', 'msg5', 'msg6', 'msg7', 'msg8', 'msg9']
-----------------------------------------
data sent: b'msg3'
Removing data: msg3
['msg4', 'msg5', 'msg6', 'msg7', 'msg8', 'msg9']
-----------------------------------------
data sent: b'msg4'
Removing data: msg4
['msg5', 'msg6', 'msg7', 'msg8', 'msg9']
-----------------------------------------
Connection lost!!!!!!.
data sent: b'msg5'
Removing data: msg5
['msg6', 'msg7', 'msg8', 'msg9']
-----------------------------------------
data sent: b'msg6'
Removing data: msg6
['msg7', 'msg8', 'msg9']
-----------------------------------------
data sent: b'msg7'
Removing data: msg7
['msg8', 'msg9']
-----------------------------------------
data sent: b'msg8'
Removing data: msg8
['msg9']
-----------------------------------------
socket.send() raised exception.
data sent: b'msg9'
Removing data: msg9
[]
-----------------------------------------
All list was sent to the server.