[Twisted-Python] LineReceiver
![](https://secure.gravatar.com/avatar/a429f507fa1caa56f072f13c55120ec7.jpg?s=120&d=mm&r=g)
Hello all, I am building a basic chat program. I am using LineReceiver for the protocol on both the server and the client. My problem is when i loop through all the clients in my ServerFactory and choose to send data to the client with multiple calls to transport.write(data), the client side does not pick it up in two seperate calls, instead it is received in one lineReceive. In the server I call transport.write() twice, the client however receives both calls in one lineReceive instead of two lineRecieve calls, how can I get lineReceive to be fired for each transported message? //// CLIENT LINE RECEIVE //// def lineReceived(self, line): if self.app: self.app.addMessage(line) //// SERVER CONNECTION MADE //// def connectionMade(self): self.factory.clients.append(self) host = self.transport.getPeer().host + ": joined the server." for c in self.factory.clients: c.transport.write(host + '\n') if self == c: c.transport.write(self.getMOTD()) ### getMOTD just returns some string ###
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Sun, 25 Feb 2007 18:26:33 -0500, Lee Connell <lee.a.connell@gmail.com> wrote:
The purpose of LineReceiver is to take a stream of bytes which no guarantees about how many bytes will be received at a time and make it appear as a line stream where each complete line is delivered separately. Since you are using LineReceiver, rather than calling self.transport.write, you should probably be using self.sendLine, which will insert the delimiter bytes for you. You must also be careful not to include the delimiter bytes in the strings you pass to self.sendLine.
So, consider doing this instead: c.sendLine(host)
As well as: c.sendLine(self.getMOTD()) Also note that the default LineReceiver line delimiter is \r\n, not \n. Jean-Paul
![](https://secure.gravatar.com/avatar/5afc738b609bc29d0ac60b3b91a8dd81.jpg?s=120&d=mm&r=g)
Jean-Paul Calderone wrote:
Also note that the default LineReceiver line delimiter is \r\n, not \n.
Lee, If for some reason you need to transmit messages which include \r\n or \n, then you can easily change the delimiter in your protocol to e.g. "\x04" (EOT, End Of Transmission). from twisted.protocols import basic class MyLineReceiverProtocol(basic.LineReceiver): delimiter = "\x04" Remember to perform this modification both clientside and serverside. More info: http://twistedmatrix.com/documents/current/api/twisted.protocols.basic.LineR... There's also LineOnlyReceiver: "A protocol that receives only lines. This is purely a speed optimisation over LineReceiver, for the cases that raw mode is known to be unnecessary." http://twistedmatrix.com/documents/current/api/twisted.protocols.basic.LineO... Cheers, Einar
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Sun, 25 Feb 2007 18:26:33 -0500, Lee Connell <lee.a.connell@gmail.com> wrote:
The purpose of LineReceiver is to take a stream of bytes which no guarantees about how many bytes will be received at a time and make it appear as a line stream where each complete line is delivered separately. Since you are using LineReceiver, rather than calling self.transport.write, you should probably be using self.sendLine, which will insert the delimiter bytes for you. You must also be careful not to include the delimiter bytes in the strings you pass to self.sendLine.
So, consider doing this instead: c.sendLine(host)
As well as: c.sendLine(self.getMOTD()) Also note that the default LineReceiver line delimiter is \r\n, not \n. Jean-Paul
![](https://secure.gravatar.com/avatar/5afc738b609bc29d0ac60b3b91a8dd81.jpg?s=120&d=mm&r=g)
Jean-Paul Calderone wrote:
Also note that the default LineReceiver line delimiter is \r\n, not \n.
Lee, If for some reason you need to transmit messages which include \r\n or \n, then you can easily change the delimiter in your protocol to e.g. "\x04" (EOT, End Of Transmission). from twisted.protocols import basic class MyLineReceiverProtocol(basic.LineReceiver): delimiter = "\x04" Remember to perform this modification both clientside and serverside. More info: http://twistedmatrix.com/documents/current/api/twisted.protocols.basic.LineR... There's also LineOnlyReceiver: "A protocol that receives only lines. This is purely a speed optimisation over LineReceiver, for the cases that raw mode is known to be unnecessary." http://twistedmatrix.com/documents/current/api/twisted.protocols.basic.LineO... Cheers, Einar
participants (4)
-
"Einar S. Idsø"
-
Jarrod Roberson
-
Jean-Paul Calderone
-
Lee Connell