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

if on the "server" side i have the code :
#!/usr/bin/perl -Tw use strict; BEGIN { $ENV{PATH} = '/usr/ucb:/bin' } use Socket; use Carp; my $EOL = "\015\012"; sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } my $port = shift || 2345; my $proto = getprotobyname('tcp'); ($port) = $port =~ /^(\d+)$/ or die "invali +d port"; socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $! +"; setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!"; bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!"; listen(Server,SOMAXCONN) || die "listen: $!"; logmsg "server started on port $port"; my $paddr; $SIG{CHLD} = \&REAPER; for ( ; $paddr = accept(Client,Server); close Client) { my($port,$iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr,AF_INET); logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port"; print Client "Hello there, $name, it's now ", scalar localtime, $EOL; }

and on the "client" side i have :

#!/usr/bin/perl -w use strict; use Socket; my ($remote,$port, $iaddr, $paddr, $proto, $line); $remote = shift || 'localhost'; $port = shift || 2345; # random port if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } die "No port" unless $port; $iaddr = inet_aton($remote) || die "no host: $remote"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; connect(SOCK, $paddr) || die "connect: $!"; while (defined($line = <SOCK>)) { print $line; } close (SOCK) || die "close: $!"; exit;

How can i most simplify it so that the "client" will simply send the "server" an array of data (@data) ,and then the server will print this @data and send the "client" $data[1], and the "client" will print it ($data[1]) ?

20040810 Edit by ysth: change title from: server/client massaging

Replies are listed 'Best First'.
Re: Client/Server Messaging
by borisz (Canon) on Aug 01, 2004 at 13:45 UTC
    Here is a example that does it.

    Server

    #!/usr/bin/perl use IO::All; use Storable qw/thaw/; my $s = io(':55555')->fork->accept; my $l = $s->getline; ( my $size ) = $l =~ /(^\d+)/; my $data; $s->read( $data, $size ) || die $!; my @data = @{ thaw($data) }; $s->print( $data[1] );

    Client

    #!/usr/bin/perl use IO::All; use Storable qw/freeze/; my @data = qw/hello world/; my $data = freeze( \@data ); my $io = io('localhost:55555'); $io->print( length($data), "\n", $data ); print $io->getline;
    Boris
    A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Client/Server Messaging
by exussum0 (Vicar) on Aug 01, 2004 at 13:24 UTC
    You need to serialize your data somehow.

    Serializatoin means taking your native, in memory, possibly broken up in 30 different ways in memory, to a single, encoded format that represents what was in memory. That format can be brought back deserialized back to the native format. Some of the ponters will be different, but the structure will return.

    Simplest way? It may be yaml, though I've heard a few complaints about it. It could be storable. Play with it. I'm sure you'll find a module or format that you find simple. Heck, if you are sending only arrays, CSV format may be easiest.

    Bart: God, Schmod. I want my monkey-man.

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