By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

How do I reconnect a disconnected socket?

P: n/a
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(socket.AF_INET, socket.SOCK_STREAM)
def doconn():
sock.connect(("localhost", 1234))
def dodiscon():
sock.close()
doconn()

doconn()

while (1):
buffer = sock.recv(1024)
if not buffer:
dodiscon()
Mar 28 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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.comwrote:
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(socket.AF_INET, socket.SOCK_STREAM)
def doconn():
sock.connect(("localhost", 1234))
def dodiscon():
sock.close()
doconn()

doconn()

while (1):
buffer = sock.recv(1024)
if not buffer:
dodiscon()
Mar 28 '08 #2

P: n/a
This is what I've got right now:

#! /usr/bin/env python
import socket, string
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
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

Mar 28 '08 #3

P: n/a
On Mar 28, 10:01 am, Jason Kristoff <deevine-removethis-
s...@gmail.comwrote:
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(socket.AF_INET, socket.SOCK_STREAM)
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 ReconnectingClientFactory -
http://twistedmatrix.com/trac/browse...otocol.py#L198
Mar 28 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.