SunOS 5.6 Perl 5.6.1

I have the following:

$SIG{'CHLD'} = 'IGNORE'; open(STDOUT, ">>my.log"); STDOUT->autoflush(1); open(STDERR, ">>my.log"); STDERR->autoflush(1); my $sock = new IO::Socket::INET (LocalHost => 'foo.com', LocalPort => 1234, Proto => 'tcp', Listen => 5, ReuseAddr => 1 ); $sock or die("Failed to create sock: $!."); ONE: while ( 1 ) { my $new_sock = $sock->accept; if (!defined($new_sock)) { print(STDERR "Warning: Bad connect.\n"); next ONE; } if ( ! defined($child = fork) ) { print("Could not fork: $!.\n"); exit(1); } $child && next; <do something as the child> shutdown($new_sock,2); close($new_sock); exit(0); } # End ONE close($sock);

My questions are:

1. For the close($new_sock);, should I be checking the return value for this. I wasn't sure why a close on a socket connection would fail.

2. For the close($sock);, is this necessary/useful and can/should I check for a failure? Also, would shutdown($sock,2); do anything useful? I didn't think so, but I saw it while googling for example code.

3. If the child process is executing a command like system("echo Hello");, I need to reap child processes before closing the forked child process so as not to leave "zombies" right? Or is my handling that via capturing the CHLD signal ($SIG{'CHLD'} = 'IGNORE';) before I even start up the socket sufficient? I wasn't sure I needed to do one of these:

while ( my $child_process = waitpid(-1,WNOHANG) > 0 ) { }

before I close the child each time.

4. In Perl 5.6.1, after each accepted connection do I need to reset STDERR and STDOUT that I set prior creating the socket? Previously in Perl 5.005_02 I was having to reset these with each new connection. If that's the same, can someone tell me why?

I think that's it. I thank you for any help you can offer.

Regards,

NOTE: Updated to remove filehandle comment so we can better focus on the questions. Thanks -sk

Shannon Kerr

2005-10-21 Retitled by planetscape, as per Monastery guidelines
Original title: 'Socket'


In reply to Handling sockets in a server by skerr1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.