Re: Network in Python

Mostly by playing around with the examples, also reading through [1500 archers] for a better understanding of lock step networking and such. In simplist terms, the factory handles the connection process and calls a client class for handling connections when another computer reaches out, for example here's a basic TCP/IP server:

from twisted.internet import reactor, protocol

class Echo(protocol.Protocol):
    """This is just about the simplest possible protocol"""
    
    def dataReceived(self, data):
        "As soon as any data is received, write it back."
        print("Client Said: ",data)
        self.transport.write(data)

def main():
    """This runs the protocol on port 8000"""
    factory = protocol.ServerFactory()
    factory.protocol = Echo
    reactor.listenTCP(8000,factory)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

In this example, Main() is called which sets up the initial server. The server factory handles the process of establishing a connection, and assigns a connecting computer a protocol class for sending and recieving data, in this case, the Echo() class above. Then the reactor listens on port 8000 for any computer that tries to connect, if one does it passes it to the factory to handle the connection and assigns it the Echo class to communicate with it. Now, for the client example:

from twisted.internet import reactor, protocol

class EchoClient(protocol.Protocol):
    """Once connected, send a message, then print the result."""
    
    def connectionMade(self):
        self.transport.write(b"hello, world!")
    
    def dataReceived(self, data):
        "As soon as any data is received, write it back."
        print("Server said:", data)
        self.transport.loseConnection()
    
    def connectionLost(self, reason):
        print("connection lost")
        reactor.stop()

# this connects the protocol to a server running on port 8000
def main():
    f = protocol.ClientFactory()
    f.protocol = EchoClient
    reactor.connectTCP("localhost", 8000, f)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

This client calls main(), which establishes the Client Factory, and then tries to connect to a server on port 8000, if it succeeds it calls the client factory to handle setting up the connection with the server and assigns the EchoClient() class to handle sending and recieving data.

So, so sum it up: both the client and the server listen and reach out through ports, when a connection is detected they use factories to handle those incoming connections, and then assign them class handlers for sending and recieving data. When someone connects to a server, it creates an Echo class which you can stuff in a list somewhere, and then to use it you do something like:

self.clients[0].send("message for client one.")
-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : pauliyobo via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Hijacker via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : kianoosh via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : visualstudio via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector

Reply via email to