I need to write a gateway server that forwarding data between client and data server¡£I use a Protocol instance for incoming connection from client, and creating another Protocol instance to connect data server when received data from client:<br>
<br>Exp.1<br>...<br>Class ToClientProtocol(Protocol):<br>""" Protocol for incoming connection from client. """<br> <br> def connectionMade(self):<br> <br> self.data_server = reactor.connectTCP('localhost', 8888, DataServerFactory()) # make connection to data server<br>
<br> self.data_server.transport.write('hello!') # send a message to data server, but not work<br>...<br><br>The connetion to data server can be made, BUT the data server received nothing. self.data_server.transport.write() not work.<br>
<br>It will work if I write like that:<br>Exp. 2 <br>...<br>Class ToClientProtocol(Protocol):<br>
""" Protocol for incoming connection from client. """<br>
<br>
def connectionMade(self):<br>
<br>
self.data_server = reactor.connectTCP('localhost', 8888, DataServerFactory(self)) # make connection to data server<br>
<br>
Class DataServer(Protocol):<br>""" Protocol for connection to data server. """<br><br> def __init__(self, client):<br> self.client = client<br><br> def connectionMade(self):<br>
self.client.data_server.transport.wirte('hello!') # send message to data server, it's work now!<br><br>Class DataServerFactory(self):<br><br> def __init__(self, client):<br> self.client = client<br>
<br> def buildProtocol(self, addr):<br> return DataServer(self.client)<br>...<br><br>I don't think this is the best way. Why I can't simply wirte like Exp.1?<br><br>