LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-27-2009, 07:36 AM   #1
krishna123
LQ Newbie
 
Registered: Apr 2008
Location: New Delhi
Posts: 4

Rep: Reputation: 0
tcp connection is alive or not


how can I check the connection is alive or not in socket programming?
 
Old 10-27-2009, 11:21 AM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,538

Rep: Reputation: 58
I dont really understand the question. What do you mean when you say "connection alive"? 1) Does it mean that the computer is attached to some network? 2) Does it mean it is connected to a specific network, i.e. the internet? 3) Or does it simply mean that you can use [your programming language here, as you didnt specify which one] to create a TCP socket? 4) Does it mean that some socket is open and accessible?

1) You could try and connect to a resource on its network (i.e. its default gateway or other resource that is guaranteed to be accessible).
2) Similar to 1), but use an internet resource (i.e. google.com).
3) Simply type the code to create some TCP socket and see if it fails.
4) Create a socket to the host and port in question, and try to connect to it.

Last edited by nadroj; 10-27-2009 at 11:25 AM.
 
Old 10-27-2009, 12:49 PM   #3
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 465

Rep: Reputation: 33
Type netstat
 
Old 10-27-2009, 02:45 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,538

Rep: Reputation: 58
Quote:
Originally Posted by gregorian View Post
Type netstat
The question was how to do it programmatically, i.e. not using some command-line tool.
 
Old 10-28-2009, 12:47 AM   #5
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 465

Rep: Reputation: 33
Sorry about that. Unless the OP can be more specific, I'd ask him to read about h_errno and perror.
 
Old 11-06-2009, 04:17 AM   #6
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
You need to perform some socket operation for that. I think "select" call will work too. Just call the select function on socket for read, write and error with little timeout value (like 1 millisecond) and you can check if the socket is readable, writable or there is some error.
Of course if your socket descriptor is in Error FD_SET then its not alive, and if it is readable and writable it will be alive as well

Hope it works,

Regards,
--Tayyab
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TCP KEEP ALIVE probe is sent even when there are packets exchanged before timer expir pallakke Linux - Networking 0 09-03-2009 10:44 PM
Connection Failed due to: TCP/IP ports not alive !!! penguinux Linux - Newbie 1 11-21-2008 09:23 AM
Keeping the connection alive? NonSumPisces Linux - Networking 2 07-08-2004 05:12 PM
TCP Keep alive Option Performance impact ndwivedi Linux - Software 0 06-24-2004 01:58 AM
imap+xinetd: connection not kept alive foogy Linux - Networking 3 07-08-2003 03:18 PM


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

Main Menu
 
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
Open Source Consulting | Domain Registration