How do I reconnect a disconnected socket?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Kristoff

    How do I reconnect a disconnected socket?

    I'm trying to make something that once it is disconnected will
    automatically try to reconnect. I'll add some more features in later so
    it doesn't hammer the server but right now I just want to keep it simple
    and get that part working. The problem is that when I use sock.close I
    get an error message of
    Bad File Descriptor
    and if I either use shutdown or just go straight to reconnecting I get:
    Transport endpoint is already connected

    This is what I've got right now:

    #! /usr/bin/env python
    import socket, string
    sock = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    def doconn():
    sock.connect((" localhost", 1234))
    def dodiscon():
    sock.close()
    doconn()

    doconn()

    while (1):
    buffer = sock.recv(1024)
    if not buffer:
    dodiscon()
  • pianomaestro@gmail.com

    #2
    Re: How do I reconnect a disconnected socket?


    Did you try just creating a new socket every time you do a connect ?

    On Mar 28, 10:01 am, Jason Kristoff <deevine-removethis-
    s...@gmail.comw rote:
    I'm trying to make something that once it is disconnected will
    automatically try to reconnect. I'll add some more features in later so
    it doesn't hammer the server but right now I just want to keep it simple
    and get that part working. The problem is that when I use sock.close I
    get an error message of
    Bad File Descriptor
    and if I either use shutdown or just go straight to reconnecting I get:
    Transport endpoint is already connected
    >
    This is what I've got right now:
    >
    #! /usr/bin/env python
    import socket, string
    sock = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    def doconn():
    sock.connect((" localhost", 1234))
    def dodiscon():
    sock.close()
    doconn()
    >
    doconn()
    >
    while (1):
    buffer = sock.recv(1024)
    if not buffer:
    dodiscon()

    Comment

    • Laszlo Nagy

      #3
      Re: How do I reconnect a disconnected socket?

      This is what I've got right now:
      >
      #! /usr/bin/env python
      import socket, string
      sock = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      def doconn():
      sock.connect((" localhost", 1234))
      def dodiscon():
      sock.close()
      doconn()
      >
      doconn()
      >
      while (1):
      buffer = sock.recv(1024)
      if not buffer:
      dodiscon()
      >
      sock.recv(1024) can return zero bytes of data indicating that no data
      arrived yet. It does not mean that you have been disconnected. This is
      especially true when you do nothing but recv, recv, recv() in an
      infinite loop.

      I recommend that you use select.select to see if there is some data that
      can be read. Call socket.recv() only when you know that it will not fail.

      Best,

      Laszlo

      Comment

      • Mike

        #4
        Re: How do I reconnect a disconnected socket?

        On Mar 28, 10:01 am, Jason Kristoff <deevine-removethis-
        s...@gmail.comw rote:
        I'm trying to make something that once it is disconnected will
        automatically try to reconnect. I'll add some more features in later so
        it doesn't hammer the server but right now I just want to keep it simple
        and get that part working. The problem is that when I use sock.close I
        get an error message of
        Bad File Descriptor
        and if I either use shutdown or just go straight to reconnecting I get:
        Transport endpoint is already connected
        >
        This is what I've got right now:
        >
        #! /usr/bin/env python
        import socket, string
        sock = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
        def doconn():
        sock.connect((" localhost", 1234))
        def dodiscon():
        sock.close()
        doconn()
        >
        doconn()
        >
        while (1):
        buffer = sock.recv(1024)
        if not buffer:
        dodiscon()
        I'd recommend to look at Twisted ReconnectingCli entFactory -

        Comment

        Working...