LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-05-2007, 02:23 AM   #1
Kristofer
LQ Newbie
 
Registered: Nov 2005
Location: Sweden
Distribution: Slackware
Posts: 20

Rep: Reputation: 0
FD_ISSET signals new data on remotely closed socket


Hi

I'm having trouble with fd_set's, select and the FS_ISSET macro. I have two separate fd_set's one for the listen socket and one for connected sockets. Once I have accepted a connection that socket is added to the fd_set of connected sockets. A message is then sent by the remote client, FD_ISSET(...) recognizes this and I read the data. The problem is that once the remote socket has been closed FD_ISSET(...) keeps signaling that I have new data waiting on this socket, but when I try to read it I get a return value of 0 which is correct for a disconnected socket. I noticed this because I first had forgotten to remove the socket from the fd_set, which I now have corrected, but the question remains, why does FD_ISSET(...) keep signaling new data on a closed socket?

/Kristofer
 
Old 04-06-2007, 05:44 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
I don't know the internals of the TCP stack but my guess is that you don't close the socket once you've found that the other side has disconnected (read returns 0).
 
Old 04-06-2007, 06:29 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
This behavior is by design. Here's a short explanation:
http://www.developerweb.net/forum/ar...hp/t-3316.html
 
Old 04-06-2007, 07:58 PM   #4
Kristofer
LQ Newbie
 
Registered: Nov 2005
Location: Sweden
Distribution: Slackware
Posts: 20

Original Poster
Rep: Reputation: 0
The question is now solved

Thx for the good replies, helped me find the explanation I was looking for:
http://lkml.org/lkml/2002/6/10/68.

Basically it's a implementation dispute, where one implementation exists on Linux and another on "Other UNIX".

Here are some citations from the discussions in the above mentioned thread:
"The problem stems from the fact that Linux does not make distinction between 'not yet connected' and 'already closed' states.

What you say is true and correct for 'already closed' state. In this case
select _must_ return 'ready to read' at least once so that application can
properly sense EOF condition. What happens on subsequent select's/read's is
strictly speaking implementation defined (access to a file after EOF has
been seen is undefined) but natural implementation is "sticky EOF" that
always returns EOF after socket has been closed.
"

Example output from a test application:
"Linux runs:

bor@cooker% ./a.out
testing with a normal, unconnected socket:
socket is readable
socket is writeable
created socket, select() returned 2
read() returned 0, errno = 0 (Success)
write() returned -1, errno = 32 (Broken pipe)
testing with a non-blocking, unconnected socket:
socket is readable
socket is writeable
socket nonblocking, select() returned 2
read() returned 0, errno = 0 (Success)
write() returned -1, errno = 32 (Broken pipe)

"Other UNIX" runs:

bor@itsrm2% /tmp/a.out
testing with a normal, unconnected socket:
created socket, select() returned 0
read() returned -1, errno = 134 (Transport endpoint is not connected)
write() returned -1, errno = 134 (Transport endpoint is not connected)
testing with a non-blocking, unconnected socket:
socket nonblocking, select() returned 0
read() returned -1, errno = 134 (Transport endpoint is not connected)
write() returned -1, errno = 134 (Transport endpoint is not connected)"
 
  


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
Streams with closed caption data. abirami Linux - Software 0 11-12-2006 10:44 PM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
how do i know if the connection is closed using a nonblocking socket? Thinking Programming 2 10-06-2005 08:53 AM
how to find out if a nonblocking socket is closed jwstric2 Programming 3 04-03-2005 10:14 AM
tightVNC - RFB socket closed abapdr Linux - General 0 05-26-2004 03:26 AM

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

All times are GMT -5. The time now is 12:14 PM.

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