LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2010, 11:44 AM   #1
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 52

Rep: Reputation: 17
How to detect broken connection before sending data with sockets


Hi, I am writing a code in Python where a socket client changes data with the server. That works nice if the connection is up. However it is also supposed to work offline. So I need to be able to detect if the connection is up before sending data, but I was not able to do so.

A summary of the code is like that:

Code:
try:
 socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 socket.connect( (self.host, self.port) )
 connected = True
except:
 connected = False

if connected:
 data = "some data byte"
 try:
  socket.send(data)
 except:
  connected=False
  socket.close()
If I unplug the cable after the connection is ready then the socket sends data
and does not detect the connection failure. What is amazing is that the client detects the connection failure just after the cable has been plugged again. Of course there is a loop in the code above and always that connected==False a new connection is made.

How could the program detect the connection failure before sending data
and then lead the code to an exception?
Thanks
 
Old 10-29-2010, 07:19 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
For tcp there really isn't much you can do, you could turn on tcp keepalive which is effectively just sending data to determine if the connection is up or not, but the default tcp keepalive is a large window, you'll probably know quicker sending your own data.
 
Old 11-02-2010, 08:55 PM   #3
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 52

Original Poster
Rep: Reputation: 17
Thanks for your reply estabroo. Do you mean, the server sending a packet to confirm the connection. Like a ping packet? I thought in defining a kind of timeout. Then if after some time that the client (python code) has sent a packet there is no reply from the server, the client supposes the packet was not sent and the connection was down.
 
Old 11-03-2010, 04:59 AM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Yeah kind of like a ping packet, several protocols use them both to keep firewall natting associations up and to help determine when a connection drops. A timeout like you described should work just fine.
 
  


Reply

Tags
python, socket



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending and receiving data simultaneously using BSD sockets nagaraju1024 Linux - Networking 2 06-27-2010 10:17 PM
broken pipe/connection reset by peer - error writing data to the network slozzy Linux - Server 2 10-04-2008 11:03 AM
Sending files over udp sockets in C raszagal Programming 9 11-27-2004 11:49 AM
Broken pipe error when sending data on LAN Tanc Linux - Networking 2 09-14-2003 05:16 AM
Sending data after establishing PPP connection ruchika Linux - Software 0 09-05-2003 07:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 11:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration