Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Networking
User Name
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.


  Search this Thread
Old 09-11-2011, 03:09 PM   #1
Registered: Mar 2007
Location: Stuttgart, Germany
Distribution: gentoo
Posts: 72

Rep: Reputation: 15
linux programing: checking of socket connection is alive


I am writing a daemon that is servecing TCP-socket connections. I want the communication to reliable, i.e. if the connection breaks for whatever reason, I want the daemon to just wait for the next connection, and the client to automatically reconnect.

How do I do this ?

1) Do I need to close and reopen the socket, on which I am listing for inbound connection request ?

a) If I kill the client, the server either terminates, or receives garbage. Sometimes it works fine, i.e. I kill the client, restart the client, and it reconnects just fine

The failing behavior might have something to do with the socket not being closed yet (daemon listens on port 9600):

netstat -t
tcp 0 0 ztron:9600 SYN_RECV
tcp 0 0 ztron:9600 SYN_RECV
tcp 17 0 ztron:9600 CLOSE_WAIT
tcp 17 0 ztron:9600 CLOSE_WAIT

b) If I kill the server, the client often subsequently terminates as well. The exit code is 141 which is none of the arguments given to exit statements. The program either exits with exit(0) or exit(-1).

I guess the issue is that neither the server or the client realize that there is nobody on the other end of the socket any longer.

How do I find out about it ?
Right now, I am checking every send and recv command for a negative return value like this:

if (send(client->fd, buffer_tx,strlen(buffer_tx), 0) < 0)

Old 09-11-2011, 09:51 PM   #2
Registered: Jul 2007
Location: Argentina
Distribution: Ubuntu 13.04 , Debian Lenny 5.0.7
Posts: 223

Rep: Reputation: 21
I believe this thread corresponds to

You will find proper help there.


linux, programing, 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
TCP keep alive and socket termination gdkool Programming 6 11-22-2009 04:20 AM
HTTP Keep-Alive socket problem imdupeng Programming 1 03-29-2008 12:17 PM
PHP - Check if Socket is alive baddah Programming 6 12-11-2007 09:50 AM
I need some socket programing in C help tomazN Programming 6 12-08-2005 12:35 PM
Raw Socket Programing djgerbavore Programming 2 08-18-2004 01:58 PM > Forums > Linux Forums > Linux - Networking

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

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