Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

open many sockets in script

by httpd (Novice)
on Jul 04, 2012 at 20:04 UTC ( #979912=perlquestion: print w/replies, xml ) Need Help??
httpd has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, I have a simple question, I wanna open a few sockets using single script, I have a code that opens 2 sockets:
use IO::Socket; my $sock1 = new IO::Socket::INET ( PeerAddr => '127.0.0.1', PeerPort => '80', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock1; my $sock2 = new IO::Socket::INET ( PeerAddr => '127.0.0.1', PeerPort => '80', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock2;
but i want to open more in cycle, however I cannot set correctly socket variable so it can vary in cycle, look:
for ($i=1;$i<10;$i++) { my ${sock$i} = new IO::Socket::INET ( PeerAddr => '127.0.0.1', PeerPort => '389', Proto => 'tcp', ); die "Could not create socket: $!\n" unless ${sock$i}; }

but I get error: Can't declare scalar dereference in "my" at f.pl line 12, near "} =" Execution of f.pl aborted due to compilation errors.

how should I set ${sock$i} in cycle so it be $sock1 $sock2....$sock10 ?? thanks

Original content restored above by GrandFather

thanks guys, you saved me much time :)

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 (Abbot) 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
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? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2018-10-22 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (119 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!