[Kamaelia] TCPClient: How to sense connection failure?

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Sun Jan 13 11:39:02 CET 2008


Michael Sparks wrote:
> The behaviour you're seeing sounds odd (which is hopefully
> encouraging :-), but it's not clear from the description whether
> its a bug in your code or Kamaelia. One question I really have as
> a result is what version are you using?

Oh sorry, it's the versions from MegaPack 1.4.0.

> In the meantime - whilst I check to see if there's a bug I didn't
> know about, the following 2 cookbook entries may be of use:
>    * http://kamaelia.sourceforge.net/Cookbook/TCPSystems
>    * http://kamaelia.sourceforge.net/Cookbook/Carousels - allows
>    you to make
>      something that exits reusable. It's a little awkward to get
>      your head around, but is quite useful when you do. (I've
>      heard of others using Carousel & TCPClient to make a
>      reconnecting TCPClient in the past)

Thanks for all the information.
 
> All that said, I'm not going to rule out a bug and look into it.
> (if you have a simple example you find fails, please forward it to
> me :)

Sure, here is my code (but see below ;) ):

----snip----------------------------------------------------------
from Kamaelia.Internet.TCPClient import TCPClient
from Kamaelia.Chassis.Pipeline import Pipeline
from Kamaelia.Util.Console import ConsoleEchoer, ConsoleReader
from Axon.Component import component
from Axon.Ipc import shutdownMicroprocess, producerFinished

class Listener(component):

    Inboxes = {"inbox": "Inbox",
               "control": "control signals received here",
              }

    Outboxes = {"outbox": "(not used)",
                "signal": "(not used)"
               }

    def main(self):
        while True:
            if self.dataReady("inbox"):
                print "data from Inbox:", repr(self.recv("inbox"))
            if self.dataReady("control"):
                control_data = self.recv("control")
                print repr(control_data)
                if isinstance(control_data, shutdownMicroprocess):
                    print "Connection could not be established"
                    break
                elif isinstance(control_data, producerFinished):
                    print "Connection closed"
                    break
            yield 1

k = ConsoleReader(">>> ")
tcp_client = TCPClient("127.0.0.1", 1850)
listener = Listener()
Pipeline(tcp_client, listener).run()
----snip----------------------------------------------------------

So I'm just using a client and a helper object to display all data
behind it. I usually start the script in one VT, and "nc -l -p
1850" in another. Using wireshark, the packet sequence is almost
identical:

Client closes connection:
C: SYN
S: SYN,ACK
C: ACK
[connection established]
C: FIN,ACK
S: FIN,ACK
C: ACK

Client closes connection:
C: SYN
S: SYN,ACK
C: ACK
[connection established]
S: FIN,ACK
C: ACK
C: FIN,ACK
S: ACK

Looks like a perfectly normal handshake to me.

> The following code may also be useful when debugging:

Cool, I've been looking for a code piece like that. :)

Whoops, the TCP client does in fact quit if the server closes
connection :) For some reason, my Listener doesn't quit. I thought
it's sufficient to exit the main method in some way to quit a
component? That's what I do using "break" in the 
'if self.dataReady("control")' part of main.

Regards,


Björn

-- 
BOFH excuse #265:

The mouse escaped.




More information about the Python-list mailing list