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

How do I reconnect a disconnected socket?

P: 1
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
Expand|Select|Wrap|Line Numbers
  1. Bad File Descriptor
and if I either use shutdown or just go straight to reconnecting I get:
Expand|Select|Wrap|Line Numbers
  1. Transport endpoint is already connected
This is what I've got right now:
Expand|Select|Wrap|Line Numbers
  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()
  16.  
What am I doing wrong?
Mar 28 '08 #1
Share this Question
Share on Google+
1 Reply


Subsciber123
P: 87
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
Expand|Select|Wrap|Line Numbers
  1. Bad File Descriptor
and if I either use shutdown or just go straight to reconnecting I get:
Expand|Select|Wrap|Line Numbers
  1. Transport endpoint is already connected
This is what I've got right now:
Expand|Select|Wrap|Line Numbers
  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()
  16.  
What am I doing wrong?
You already closed sock. Now you cannot reuse it. You must create another socket. Here, I think that this should work:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/env python
  2. import socket, string
  3. def doconn():
  4.     sock = socket.socket() # this assumes what you stated explicitly before, as they are the default values
  5.     sock.connect(("", 1234)) # you don't have to state "localhost", an empty string will do
  6.     return sock
  7. def dorecon(sock):
  8.     sock.close()
  9.     return doconn()
  10.  
  11. sock=doconn()
  12.  
  13. while 1: # grrr! this is not lisp! try not to use parens!
  14.     buffer = sock.recv(1024)
  15.     if not buffer:
  16.         sock=dorecon(sock)
  17.  
I also renamed the second function name so that it made more sense.
btw: not that there is anything wrong with lisp, just python is not it.
Also, use [ code=python ] [ /code ] (without spaces) in order to get syntax highlighting
Apr 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.