views:

22

answers:

1

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?

A: 

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.

Glyph