[Twisted-web] sendLine and connectionLost question

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed May 26 21:38:49 EDT 2010

On 26 May, 10:46 pm, djimenez at unipost.es wrote:
>Hi all,
>I'm trying to write a typical chat server application with twisted. At 
>this moment, all is working fine except for some strange behaviour with 
>sendLine and connectionLost funcs. Let me explain the problem:
>- I have a server on the internet.
>- I connect two PCs (2 clients) from the same LAN through a router to 
>that server
>- All is working fine until I disconnect the LAN of one of those PC 
>(The client is disconnected but the server doesn't know about it). Just 
>to test a typical wireless disconnection without closing sockets, 
>- After disconnecting I try to send some messages through the remaining 
>clients to all clients through this function:
>    def sendMessageToAllClients(self, mesg,reqClient):
>        for client in self.clientProtocols:
>            dataLength = str(len(mesg))
>            if (len(dataLength) < 8):
>                dif = 8 - len(dataLength)
>                dataLength = "0"*dif + dataLength
>            print "Enviando a:",str(client)
>            client.sendLine(dataLength+mesg)
>            client.clearLineBuffer()

You shouldn't be calling `client.clearLineBuffer()` here.
>As you can see, I send a message to all clients, including the one 
>disconnected because the server doesn't
>know at this moment that the client is "out". I'm able to send 6 or 7 
>messages to the remaining client but
>after that, the connection get lost. I would expect to have a 
>connectionLost with the client offline but actually
>I got a connectionLost in both clients and I don't know why. The server 
>disconnect both clients with "non clean fashion".
>Any help or advice would be highly appreciated.

This is how TCP works.  If the remote host stops responding to packets, 
then there's a period of time where the connection still appears to be 
up anyway.  After a TCP-level timer expires, the connection is 
considered lost.


More information about the Twisted-web mailing list