473,663 Members | 2,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s ocket.AF_INET, socket.SOCK_STR EAM)
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 8449

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.comw rote:
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(s ocket.AF_INET, socket.SOCK_STR EAM)
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(s ocket.AF_INET, socket.SOCK_STR EAM)
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.comw rote:
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(s ocket.AF_INET, socket.SOCK_STR EAM)
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 ReconnectingCli entFactory -
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
15124
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 Socket.Receive() just passes without any exception. Is there any way to detect disconnection using Sockets? I believe I saw a way of doing this using IAsyncResult, and eventhandling but..no article really explained how to use it properly.
1
3655
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 the server for this. What I need is to set up a delegate to handle this disconnection (Delegate has some cleanup code). As soon as the socket is disconnected, the event should call the delegate. Thanking you, Ashwin Kambli
2
2908
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 handler or a delegate as to when the socket is disconnected - either gracefully from remote or shut down by "brute force"? In short, something like this:
1
2422
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. To simulate failure, the server disconnects the socket. However, the client socket parameter shows an open socket: socket.connected is true, socket. Active is true, socket.poll(1,selectRead) is true. When checking the MS class documentation, it is...
3
14408
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 server socket, the client is able to connect, but cannot send anything out. It seems that the Socket.Connected is false but I received no disconnection event. Any idea on how to solve the problem? Thanks.
1
3161
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 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...
0
1132
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
2206
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 using its sessionid without getting the "Connect to existing Remote Desktop" message. Like the opposite of the 'tsdiscon' command, 'tscon' doesn't get the 'server' switch?
0
2339
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 client to try to reconnect after the previous connect times out. Currently I try to re-connect on the IOErrorEvent.IO_ERROR event handler. The issue is that it seems like the socket is not trying to make another connection. I've seen the following...
0
8437
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.