View Single Post
Old Mar 28th, 2008, 10:13 AM   #1
terron
Newbie
 
Join Date: Mar 2008
Posts: 1
Rep Power: 0 terron is on a distinguished road
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:
python Syntax (Toggle Plain Text)
  1. #! /usr/bin/env python
  2. import socket, string
  3. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. def doconn():
  5. sock.connect(("localhost", 1234))
  6. def dodiscon():
  7. sock.close()
  8. doconn()
  9.  
  10. doconn()
  11.  
  12. while (1):
  13. buffer = sock.recv(1024)
  14. if not buffer:
  15. dodiscon()

What am I doing wrong?
terron is offline   Reply With Quote