Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following server code slightly modified from pymotw website. I am trying to send the data to client bearing port no. 10000 without recieving.
But unsucessful. Is there a way to send data based on a triggered event?

What I have tried:

Here is my code:
Python
###############################################################################
#  Server
###############################################################################
import asyncio
import time

FIRST_PROCESS_PORT = 10000
SECOND_PROCESS_PORT= 30000
THIRD_PROCESS_PORT = 50000



class MyLogger(object):
    def info(self, s):
        print(s)


logger = MyLogger()

class EchoServer(asyncio.Protocol):
  
    def __init__(self):
        self.client_info = None
        
    # Begin asyncio.Protocol overrides
    def connection_made(self, transport):
        self.transport = transport
        self.client_info = self.transport.get_extra_info("peername")
        logger.info(f"connection_made from: {self.client_info}")

    def connection_lost(self, reason):
        logger.info("connection_lost: %s | %s" % (self.client_info, "Client Disconnected"))
    
    def data_received(self, data):
        print("Received: %s" % str(data))
        self.transport.write(data)
        
class EchoServer1(asyncio.Protocol):
  
    def __init__(self):
        self.client_info = None
        
    # Begin asyncio.Protocol overrides
    def connection_made(self, transport):
        self.transport = transport
        self.client_info = self.transport.get_extra_info("peername")
        logger.info(f"connection_made from: {self.client_info}")
        self.transport.write(b'\x01')
       

    def connection_lost(self, reason):
        logger.info("connection_lost: %s | %s" % (self.client_info, "Client Disconnected"))
  
    def data_write(self):
        self.transport.write('b\x01')
     

async def main():
    loop = asyncio.get_running_loop()

    server1 = await loop.create_server(
        EchoServer1, "127.0.0.1", FIRST_PROCESS_PORT
    )
    server2 = await loop.create_server(
        EchoServer, "127.0.0.1", SECOND_PROCESS_PORT
    )
    server3 = await loop.create_server(
        EchoServer, "127.0.0.1", THIRD_PROCESS_PORT
    )
    

    # serve forever
    async with server1, server2,server3:
        while True:
            await asyncio.sleep(1)


if __name__ == "__main__":
    asyncio.run(main())
Posted
Comments
Richard MacCutchan 13-Nov-22 3:32am    
"But unsucessful."
Which could mean anything. Please explain exactly what happens when you issue the send to the client.
gary@36 13-Nov-22 3:46am    
By unsucessful , I mean when I try to write to client (from server), no data is displayed in client side. Whereas if I send data from client, the same data is echoed back to client. This is evident from code (class echo server1 implements transmit only function, class echo server implements transmit & recieve function)
Richard MacCutchan 13-Nov-22 3:48am    
Then you need to use the debugger to try and find out what is happening at the client end when it receives the data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900