LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-11-2011, 03:09 PM   #1
tilman1
Member
 
Registered: Mar 2007
Location: Stuttgart, Germany
Distribution: gentoo
Posts: 61

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


Hello

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 ?

2)
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 lcu1.fritz.box:38397 SYN_RECV
tcp 0 0 ztron:9600 lcu1.fritz.box:38396 SYN_RECV
tcp 17 0 ztron:9600 lcu1.fritz.box:38479 CLOSE_WAIT
tcp 17 0 ztron:9600 lcu1.fritz.box:38395 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:

Code:
if (send(client->fd, buffer_tx,strlen(buffer_tx), 0) < 0)
{
  close(client->fd);
   client->fd=-1;
 ...
}
Thanks

Tilman
 
Old 09-11-2011, 09:51 PM   #2
marco18
Member
 
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 http://www.linuxquestions.org/questions/programming-9/

You will find proper help there.
 
  


Reply

Tags
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


All times are GMT -5. The time now is 11:47 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