Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2008, 11: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
Old Mar 28th, 2008, 12:17 PM   #2
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 445
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: How do I reconnect a disconnected socket?

Hmm well i think you may find it is because when you call socket.close()
your socket becomes blank so to speak.

Python Syntax (Toggle Plain Text)
  1. #! /usr/bin/env python
  2. import socket, string
  3.  
  4. def doconn():
  5. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  6. sock.connect(("localhost", 1234))
  7. def dodiscon():
  8. sock.close()
  9. doconn()
  10.  
  11. doconn()
  12.  
  13. while (1):
  14. buffer = sock.recv(1024)
  15. if not buffer:
  16. dodiscon()

and so by moving the declaration of your socket into your doconn() function everytime you try to connect it re-defines the socket.

I thinks this works anyway plus i've no doubt been talking aload of rubbish but there we go

Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old Mar 29th, 2008, 3:39 AM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Re: How do I reconnect a disconnected socket?

When you close the socket, it'll be closed. You can reconnect with a new socket, but you can't use the same one. When it's closed, both OSs will terminate the connection.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Mar 29th, 2008, 5:12 AM   #4
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 445
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: How do I reconnect a disconnected socket?

Quote:
Originally Posted by Jimbo View Post
When you close the socket, it'll be closed. You can reconnect with a new socket, but you can't use the same one. When it's closed, both OSs will terminate the connection.
thats what i was trying to say but just couldn't lol thanks Jimbo
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java socket Server compile errors.. n3o_X Java 3 Nov 18th, 2007 1:39 AM
Streaming Sound Over a Socket King C++ 3 Apr 7th, 2007 6:29 PM
how to check if the socket is still connected? myName C++ 1 Jun 14th, 2006 7:27 AM
Socket Server hbe02 C++ 19 May 23rd, 2006 11:14 PM
Socket problem Polyphemus_ C++ 3 Feb 28th, 2006 3:01 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC