473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:

#! /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
3 8397

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: James Yang | last post by:
Hi, I am using Sockets to connect to a remote computer and send data using Socket.Send() and receive using Socket.Receive() (block mode) . for somereason tho, when the client disconnects the...
1
by: Ashwin Kambli | last post by:
Hi, I have a simple socket application. There is a server, and there are many TCP connections to the server. Now, if any one of those connections is lost, is there a way of triggering an event on...
2
by: Vladimir Lushnikov | last post by:
Hi, I know there exist asynchronous socket methods for connecting, receiving/sending data and disconnecting - but how do you know when the socket has been terminated - can you assign an event...
1
by: Techsol | last post by:
Hi, I have synchronous communications between a server and client. To save bandwith the connection must persist. So the socket must stay open and only be re-opened in case of communications failure....
3
by: Cheryl | last post by:
Hi. I am having a problem on handling asynchronous sockets in C#. I implemented a pair of client and server sockets. The connection is ok when first connected. However, when I turned off the...
1
by: terron | last post by:
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...
0
by: davy zhang | last post by:
Python3.0rc1 windowsxp in the lib\asynchat.py def handle_write (self): self.initiate_send() def push (self, data): sabs = self.ac_out_buffer_size if len(data) sabs:
0
by: ofer1 | last post by:
I have user with 3 open session in disconnected state on the same server. I have the sessionid for each remote session with 'qwinsta' command. Is it possible to reconnect to specific session by...
0
by: Dreea | last post by:
Hello everybody I am working on a client application that connects to a C# server using the Socket class. I am trying to handle the case in which the server goes offline, and I would like the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.