dataReceived in client side doesn't work?
I have implemented a simple server-client script like this:
Server:
class Server(Protocol):
def connectionMade(self):
开发者_开发问答 while True:
self.transport.write('a')
Client
class Client(Protocol):
def dataReceived(self, data):
print data
What I expected was a string of infinite a's was printed on client window, but actually, there's nothing appeared. When I replace the while loop in Server with a finite loop, it works. So it seems like the function connectionMade needs to be terminated before the whole data can appear on Client side? Am I wrong?
You're correct. As long as connectionMade
is doing stuff, no data has yet been written to the socket. transport.write(x)
doesn't mean "immediately write 'x' to the socket", it means 'when the socket has some free buffer space, write 'x' to it'.
The example, as you phrase it:
def connectionMade(self):
while True:
self.transport.write('a')
simply allocates an infinitely large buffer full of 'a's, allocating memory until it crashes.
精彩评论