LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 03-09-2011, 09:49 AM   #1
programlight
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Rep: Reputation: 0
How to detect a closed tcp client connection when client is only receiving data


Hello everyone,
I have researched this problem in this forum and elsewhere but I haven't really found an answer to my problem.
I am writing a TCP server in C, and the server listens to incoming client connections and accepts them. It then creates a thread to handle the client. The clients are expected to only receive data from my server and not send any data. So if I use a select() call with a recv(), I believe that the recv() will just block forever since there will not be any data coming from the client. If I use a non-blocking recv(), then this will just return a 0 which tells me nothing because the client is not expected to send any data. I am not sure if I have misunderstood some socket concepts, but I need a solution to detect when the client has disconnected so that I can close the socket and stop sending data to the client. As I understand it, simple ACKs etc are not captured by the recv(), and only data sent by the client will cause recv() to return a non-zero value, so I am not sure how to know when the client has disconnected.

Last edited by programlight; 03-09-2011 at 09:54 AM. Reason: More clarification
 
Old 03-09-2011, 11:14 AM   #2
programlight
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Original Poster
Rep: Reputation: 0
I found out that recv() returns 0 when the client disconnects and -1 when no data is received, so I can use recv() and check for a return value of 0 to know that the client is disconnected. Any other return (actually only a -1 is possible in my case because the client doesn't actually send data) would lead my server to assume that the client is still connected, so it will continue to send the client data.
 
Old 03-09-2011, 02:13 PM   #3
programlight
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Original Poster
Rep: Reputation: 0
I can't tell if my assumption about recv() returning a -1 is correct anymore. In my program, when the client disconnects and then reconnects, somehow the connection is lost if more data is sent to the client. Also, recv() doesn't return a -1 anymore. I do receive a "send() failed, Resource temporarily unavailable" error. I am not sure if my statements are clear to everyone reading, but any insights would be greatly appreciated.
 
Old 03-09-2011, 06:38 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
This is a Programming qn; use the Report button to ask the Mods to move it to that forum for better response.
 
Old 03-10-2011, 04:36 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
If you use select/poll and you got an notification on read and the read returned 0, then it means that the socket has been closed from the other side.
 
Old 03-10-2011, 05:17 PM   #6
programlight
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks Mara and chrism01, however, I still am not clear about the concept. Sorry if I wasn't clear in my post.

I understand that I can use select() and read() or recv(), and if read/recv returns zero then the connection was closed by the client. If the read/recv returns a -1, then there was an error in reading data from the client.

In my case, I am not expecting my client to send any data but only to receive data that I send. I also don't want my read/recv to block as long as my client is connected, so that I can send it data. So, if I configure my socket to be nonblocking, then as long as my client is connected, read/recv() will return a -1(because client is still connected but never sending the server any data).

I have written my server code with the above assumption, but when something erroneous occurs at the client side and the connection is terminated improperly, my server doesn't recognize this (because in my assumption, when read/recv returns a -1, the server is still thinking that the client is connected).

So here's my question: How do I set a client socket in my server code to be non blocking and still be able to differentiate between a client still connected vs. an improper connection termination by the client?
 
Old 03-10-2011, 06:43 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
What about the return value from write()? It will return a -1 on error, and if the socket was closed, errno would be EPIPE.

--- rod.
 
Old 03-10-2011, 07:02 PM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
I wrote ten suggestions for non-blocking TCP I/O here. This will probably answer your questions, and possibly some you should have asked, had you known about them.
 
1 members found this post helpful.
Old 10-24-2011, 09:03 AM   #9
programlight
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Thank you all for your suggestions. I appreciate all your help! Sorry about the delayed response. I'm learning forum etiquette slowly.
 
Old 10-24-2011, 09:19 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
To summarize:

if recv returns>0, then it is not EOF
if recv returns==0, then it is EOF
if recv returns<0 and errno==EWOULDBLOCK, then it is not EOF (it happens only in non-blockin mode)
if recv returns<0 and errno!=EWOULDBLOCK, then it is something that can be considered as EOF

for write:

if write returns>0, then it is success (total or partial)
if write returns<0 and errno==EWOULDBLOCK, then it is not connection-losing
if recv returns<0 and errno!=EWOULDBLOCK, then it is something that can be considered as connection-losing

Last edited by NevemTeve; 10-24-2011 at 09:52 AM.
 
1 members found this post helpful.
  


Reply



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
Identify client process initiating TCP connection port 113 esquivalient Linux - Networking 2 12-27-2010 09:38 AM
[SOLVED] How could server detect closed client socket using TCP and c++? Lobinho Programming 6 08-10-2010 02:28 PM
Get connection closed when using Tightvnc client to connect to a virtual suse linux rmitch06500 Linux - Newbie 2 12-06-2009 07:14 AM
How do I get a local(client) reboot (init 6) when a ssh connection is closed? tuxhats Linux - Networking 4 08-28-2007 05:08 PM
connection refused, tcp betwee client and server on different pc on same lan JoeyWong Linux - Networking 1 05-14-2006 02:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:36 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
Open Source Consulting | Domain Registration