in reply to Handling sockets in a server

1. For the close($new_sock);, should I be checking the return value for this. I know close doesn't return failure for a file handle, but wasn't sure about a socket connection.
Huh?!? From perldoc -f close:
close FILEHANDLE close Closes the file or pipe associated with the file handle, retur +ning true only if IO buffers are success- fully flushed and closes the system file descriptor. Closes t +he currently selected filehandle if the argument is omitted.

It is true that people normally doesn't check closes for failure on regular files. Well, I don't. But then I generally use lexical filehandles and let perl close them for me. Whatever, this doesn't mean that close can't fail... and definitely (I have very limited experiences with sockets but) I've heard many times experienced hackers claim that sockets should be explicitly closeed and checked for failure in doing so.

Replies are listed 'Best First'.
Re^2: Handling sockets in a server
by skerr1 (Sexton) on Oct 21, 2005 at 12:58 UTC

    Not to deviate too much from the questions in this post, so you can close a filehandle and you are able to check a return value from the close? I'm sorry I haven't seen this done. I do check success/failure on closing pipes and the man page link you included talks about pipes returning a failure if one of the system calls failed. So, I believe my statement is acurate about no failure returned on a file handle.

    Hopefully, someone can help with the meat of this post.

    Thanks,

    Shannon Kerr
      Not to deviate too much from the questions in this post, so you can close a filehandle and you are able to check a return value from the close?

      "I'll go further than that, I'll get off at the depot." (*) You can use the syntax:

      close FILEHANDLE or die "Could not close FILEHANDLE: $!\n";
      to tell you what the errno varaible was set to when FILEHANDLE could not be closed.

      --

      (*) - This quote stolen with great embarrassment from Groucho Marx.

      --
      tbone1, YAPS (Yet Another Perl Schlub)
      And remember, if he succeeds, so what.
      - Chick McGee

        Geez, now you sucked me in. Can you demonstrate a block of code that this works with. I've written several tools that tried to check the return on a close filehandle and never have I seen an error. I would like to see a true failure.

        And if someone would like to answer the orginal questions of the post, I would like that too! :)

        Thanks!

        Shannon Kerr
      I have seen someone do, instead. But indeed I've always considered it to be overly paranoid. The point being that a close on a regular filehandle may well fail, but we're confident that it will happen rarely enough no to go mad about it...
Re^2: Handling sockets in a server
by skerr1 (Sexton) on Oct 21, 2005 at 17:25 UTC

    OK, so it appears you are answering question #2 for closing the socket (not a connection). Can someone tell me when doing the close($sock) what are the possible reasons for failing to close the socket? How about possible reasons for failing to close a socket connection close($new_sock)?

    Is shutdown($sock,2) useful? What does it do as it's not communicating with a client?

    How about any answers for questions 3 and 4?

    Thank you

    Shannon Kerr

      close on a socket could fail if the file descriptor has already been reaped [EBADF]. [EINTR] is also possible.

      close is different from shutdown because close only cleans up the file descriptor in the current process, but shutdown will terminate the connection for all processes using it.

      That is to say, if you fork a child process that has an open socket, closing it within either process will free the file descriptor associated with that socket, within that process, but the other process will be free to continue using it's copy of the file descriptor and the socket.

      However, shutdown will terminate the connection for all processes that have a file descriptor associated with it. The file descriptor will remain after shutdown until they are explicitly closed or otherwise GC'd.

      Not having a real fork available, I'll leave questions 3 & 4 to others :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Thanks for the information. This was helpful. Can you tell me though what is the difference in return for closing the socket as apposed to the socket connection? What value am I getting out of closing each?

        close($new_sock); # Closes connection, I think your statement on return values applies to this close

        But what about

        close($sock); # What value am I getting out of checking the close of this, the actual socket, not a connection?

        Still waiting for 3 and 4 :)

        Shannon Kerr