Your question seems rather strange to me because this would be rather unusual. If you are building a server, then normal would be to have a single "listen or passive" socket. The server listens on that single socket and dispatches children as the requests come in (the server code itself is short and to the point). You can tell the OS that you allow multiple requests to be pending via the listen=>10 parameter when you create the listen (passive) socket.

Once you accept the connection from the "listen socket", it becomes an "active socket" and the server keeps "listening".

Here is the basic "server framework" for a simple application.

#!/usr/bin/perl -w use strict; use IO::Socket; use POSIX ":sys_wait_h"; my $RW_BUF_LEN = 256; my $SERVER_TIMEOUT_SECS = 20; my $active; # note SOMAXCONN is system max of queue for incoming clients # Listen=>1 in this app would have also have been just fine $SIG{PIPE} = sub {close $active; exit (3)}; $SIG{ALRM} = sub {close $active; exit (7)}; $SIG{CHLD} = sub {local ($!, $?); while (waitpid(-1, WNOHANG) > 0){} }; my $passive = IO::Socket::INET->new( Proto => 'tcp', LocalPort => 12455, Listen => SOMAXCONN, Reuse => 1, Type => SOCK_STREAM ) or die "Server is unable to start: $!\n"; while(1) { $active = $passive->accept() or next; #blocking "wait" # next not really necessary # Perl > 5.8 has deferred signals by default on # so a signal cannot occur within accept()! # but safety is a good thing! die "Bad Fork! $!\n" if ( !defined(my $pid = fork()) ); if ($pid != 0) # Parent, Note: Windows has a negative $pid # (fork emulation) { close $active; #parents do not talk to anybody next; } ####### we are the child ######### close $passive; #Client's don't listen for connections! #### blah whatever the child does..... ## it "talks" on the $active socket's filehandle }

In reply to Re: open many sockets in script by Marshall
in thread open many sockets in script by httpd

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.