LinuxQuestions.org
Visit the LQ Articles and Editorials section
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, 12:44 PM   #1
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 48

Rep: Reputation: 16
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, 08:19 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,095
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
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, 09:55 PM   #3
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 48

Original Poster
Rep: Reputation: 16
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, 05:59 AM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,095
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:17 PM
broken pipe/connection reset by peer - error writing data to the network slozzy Linux - Server 2 10-04-2008 12:03 PM
Sending files over udp sockets in C raszagal Programming 9 11-27-2004 12:49 PM
Broken pipe error when sending data on LAN Tanc Linux - Networking 2 09-14-2003 06:16 AM
Sending data after establishing PPP connection ruchika Linux - Software 0 09-05-2003 08:59 PM


All times are GMT -5. The time now is 05:56 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration