develooper Front page | perl.beginners | Postings from February 2002

Re: Sockets in an array

Thread Previous
From:
John W. Krahn
Date:
February 22, 2002 15:33
Subject:
Re: Sockets in an array
Message ID:
3C76D55B.EB85F109@acm.org
Girish Chandran wrote:
> 
> Hi,
> 
> I have the following problem.
> 
> Context:
> I am trying to open multiple sockets to multiple IP address.
> I want to use the same piece of code to do that.
> The IP addresses and port numbers are stored in a file.
> 
> The program:
> I read the file, open the sockets in sequence.
> The sockets (globs/handles) are stored in an array.
> 
> The error:
> However when I try to write to the sockets by using the array index, I get
> an error.
> 
> Here is the code snippet.

use warnings;
use strict;

> use IO::Socket ;
> $i = 0;

my $i = 0;

> foreach $line (@address){

for my $line ( @address ) {

>    chop ($line);

    chomp $line;

>    ($host, $port) = split(/\|/, $line);

    my ( $host, $port ) = split /\|/, $line;

>    $address = $host. ":" .$port;

    my $address = "$host:$port";

>    $newsocket[$i] = IO::Socket::INET->new("$address") or die $@;

    $newsocket[$i] = IO::Socket::INET->new( $address ) or die $@;

> # The sockets are opened fine.
> # Print a command to the socket.
>    print $newsocket[$i] $command;

perldoc -f print
[snip]
       Note that if you're storing FILEHANDLES in an
       array or other expression, you will have to use a
       block returning its value instead:

           print { $files[$i] } "stuff\n";
           print { $OK ? STDOUT : STDERR } "stuff\n";



    print { $newsocket[$i] } $command;

>    $i++;
> }



John
-- 
use Perl;
program
fulfillment

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About