@@ -446,22 +446,27 @@ TCP echo client example, send data and wait until the connection is closed::
446446
447447 import asyncio
448448
449- class EchoClient(asyncio.Protocol):
450- message = 'This is the message. It will be echoed.'
449+ class EchoClientProtocol(asyncio.Protocol):
450+ def __init__(self, message, loop):
451+ self.message = message
452+ self.loop = loop
451453
452454 def connection_made(self, transport):
453455 transport.write(self.message.encode())
454- print('data sent: {}'.format(self.message))
456+ print('Data sent: {!r }'.format(self.message))
455457
456458 def data_received(self, data):
457- print('data received: {}'.format(data.decode()))
459+ print('Data received: {!r }'.format(data.decode()))
458460
459461 def connection_lost(self, exc):
460- print('server closed the connection')
461- asyncio.get_event_loop().stop()
462+ print('The server closed the connection')
463+ print('Stop the event lop')
464+ self.loop.stop()
462465
463466 loop = asyncio.get_event_loop()
464- coro = loop.create_connection(EchoClient, '127.0.0.1', 8888)
467+ message = 'Hello World!'
468+ coro = loop.create_connection(lambda: EchoClientProtocol(message, loop),
469+ '127.0.0.1', 8888)
465470 loop.run_until_complete(coro)
466471 loop.run_forever()
467472 loop.close()
@@ -481,7 +486,7 @@ TCP echo server example, send back received data and close the connection::
481486
482487 import asyncio
483488
484- class EchoServer (asyncio.Protocol):
489+ class EchoServerClientProtocol (asyncio.Protocol):
485490 def connection_made(self, transport):
486491 peername = transport.get_extra_info('peername')
487492 print('Connection from {}'.format(peername))
@@ -494,11 +499,12 @@ TCP echo server example, send back received data and close the connection::
494499 print('Send: {!r}'.format(message))
495500 self.transport.write(data)
496501
497- print('Close the socket')
502+ print('Close the client socket')
498503 self.transport.close()
499504
500505 loop = asyncio.get_event_loop()
501- coro = loop.create_server(EchoServer, '127.0.0.1', 8888)
506+ # Each client connection will create a new protocol instance
507+ coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
502508 server = loop.run_until_complete(coro)
503509
504510 # Server requests until CTRL+c is pressed
@@ -575,7 +581,7 @@ method, send back received data::
575581
576582 import asyncio
577583
578- class EchoServerClientProtocol :
584+ class EchoServerProtocol :
579585 def connection_made(self, transport):
580586 self.transport = transport
581587
@@ -587,8 +593,9 @@ method, send back received data::
587593
588594 loop = asyncio.get_event_loop()
589595 print("Starting UDP server")
596+ # One protocol instance will be created to serve all client requests
590597 listen = loop.create_datagram_endpoint(
591- EchoServerClientProtocol , local_addr=('127.0.0.1', 9999))
598+ EchoServerProtocol , local_addr=('127.0.0.1', 9999))
592599 transport, protocol = loop.run_until_complete(listen)
593600
594601 try:
0 commit comments