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

#!Perl #use strict; use IO::Socket; #use Win32::SerialPort; $|++; print "how many"; $a=<STDIN>; $i=0; while($i <= $a){ print "ip"; $b[$i]=<STDIN>; chomp($b[$i]); print "port"; $p[$i]=<STDIN>; chomp($b[$i]); $i++; } $i=0; while($i <= $a){ $c[$i]=new IO::Socket::INET(PeerAddr=>$b[$i], PeerPort=>$p[$i], Prot +o=>'tcp') || warn "Can not connect: $! $_"; $c[$i]->recv($buffer,1024); print $buffer; $i++; }
Okay would I be able to get my socket connections into an array, and the use those connectons? Please help oh great ones

janitored by ybiC: Balanced <code> tags around codeblock, as per Monastery convention

Replies are listed 'Best First'.
Re: Sockets Help
by castaway (Parson) on Aug 24, 2004 at 05:30 UTC
    You appear to have the socket connections in an array, the array @c. Maybe you can show what problems you are having with actually using them? (And what exactly you want to do?) If you would like to be able to read from any of them when data is available, I suggest using IO::Select.

    C.

Re: Sockets Help
by NetWallah (Canon) on Aug 24, 2004 at 05:34 UTC
    Yes, you would be able to get an array of sockets.

    To make them usable, you would need to use either non-blocking reads, or threads.

    Here is your code made more readable/idiomatic/perlish:

    use strict; use IO::Socket; #use Win32::SerialPort; $|++; print "how many? "; my $PortCount= <STDIN>; chomp $PortCount; my (@sockinfo, $buffer); for (1..$PortCount){ my %sock; print "ip? "; $sock{IP} =<STDIN>; chomp($sock{IP}); print "port? "; $sock{PORT}=<STDIN>; chomp($sock{PORT}); push @sockinfo, \%sock; } foreach(@sockinfo){ ## print "Opening Socket to ",$_->{IP}," port ", $_->{PORT},"\n"; $_->{SOCKET}= IO::Socket::INET->new (PeerAddr=>$_->{IP}, PeerPort=>$_->{PORT}, Proto=>'tcp' +) or die "Can not connect: $@ \n IP=" . $_->{IP} . " Port=" . $_->{PORT}; $_->{SOCKET}->recv($buffer,1024); print $buffer; }

        Earth first! (We'll rob the other planets later)

      Thanks for the help that worked great.