Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

open many sockets in script

by httpd (Novice)
on Jul 04, 2012 at 20:04 UTC ( [id://979912]=perlquestion: print w/replies, xml ) Need Help??

httpd has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: open many sockets in script
by ig (Vicar) on Jul 04, 2012 at 21:02 UTC

    A my variable used inside the loop is local to the loop: you don't need a different variable name for each iteration through the loop so you can just use my $sock.

    If you want to create all the sockets and have them accessible outside the loop, then you should declare the variable outside the loop and set it inside the loop. In this case, if you want to use integers to access the various sockets I suggest you use an array, rather than differently named variables. Something like:

    my @sockets; foreach my $i (0..9) { $sockets[$i] = new IO::Socket::INET( PeerAddr => '127.0.0.1', PeerPort => '389', Proto => 'tcp', ); die "Could not create socket: $!\n" unless($sockets[$i]); } # Use the sockets here as $sockets[1], $sockets[2], etc.
      thanks guys, you saved me much time :)
        Howdy!

        ...and now you're wasting ours by not leaving the question up so the answers make sense.

        yours,
        Michael
Re: open many sockets in script
by superfrink (Curate) on Jul 04, 2012 at 21:05 UTC
    I would store the sockets in a data structure like an array instead of dynamically creating variable names. eg:
    use strict; use Data::Dumper; my @sock_list; for (my $i=0;$i<10;$i++) { $sock_list[$i] = "..." . $i; } print Dumper(\@sock_list);
    Update: ++ig
Re: open many sockets in script
by Marshall (Canon) on Jul 07, 2012 at 01:57 UTC
    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 }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://979912]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-19 04:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found