View Single Post
Old Mar 28th, 2008, 11:17 AM   #2
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
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
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote