Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: socket help

by ikegami (Patriarch)
on Feb 14, 2011 at 02:44 UTC ( [id://887912]=note: print w/replies, xml ) Need Help??


in reply to socket help

use IPC::Open3 qw( open3 ); while (my $client = $socket->accept()) { while (<$client>) { chomp; my ($cmd, @args) = split(/:/, $_); my @cmd; if ($cmd eq 'ls') { @cmd = ( 'ls', '-s', '--', @args ); } if (@cmd) { open(local *C_STDIN, '<', '/dev/null') or die; open(local *C_STDOUT, '>&, $client) or die; my $pid = open3('<C_STDIN', '>C_STDOUT', '>C_STDOUT', @cmd); waitpid($pid, 0); } } }

Replies are listed 'Best First'.
Re^2: socket help
by Anonymous Monk on Feb 14, 2011 at 14:11 UTC
    I tried the suggestion and still got nothing back.
    client code:
    #!/usr/bin/env perl use IO::Socket; $remote_port=7071; $remote_host="localhost "; $socket = IO::Socket::INET->new(PeerAddr => $remote_host, PeerPort => $remote_port, Proto => "tcp", Type => SOCK_STREAM) or die "Couldn't connect to $remote_host:$remote_port : $@\n"; $socket->autoflush(1); # ... do something with the socket print $socket "ls:/tmp\n"; while (<$socket>) { print "$_\n"; } # and terminate the connection when we're done close($socket);

    Server code:
    #!/usr/bin/env perl # perl modules use IO::Socket; use POSIX qw(:sys_wait_h); use Sys::Hostname; use IPC::Open3 qw( open3 ); # get hostname my $hostname = hostname; # Port my $port = "7071"; $SIG{CHLD} = 'IGNORE'; # make socket connection my $socket = new IO::Socket::INET ( LocalHost => "$hostname", LocalPort => "$port", Proto => 'tcp', Reuse => 1, Listen => SOMAXCONN, ); die "Could not create socket: $!\n" unless $socket; # allow server to accept clients while ($client = $socket->accept()) { # flush input $client->autoflush(1); # input from client while (<$client>) { chomp; # read command and put rest in array my ($command,@var2) = split(":", $_); my $cmd; if ($command eq 'ls'){ @cmd = ( 'ls', '-s', '--', @var2 ); } # run command if (@cmd) { open(local *C_STDIN, '<', '/dev/null') + or die; open(local *C_STDIN, '>&', $client) or + die; my $pid = open3('C_STDIN', '>C_STDOUT' +, 'C_STDOUT', @cmd); waitpid($pid, 0); } } # close client close($client); } # close socket so it can reused close($socket);
      That's not the code I posted at all, and you're hiding potential error messages by not using use strict; use warnings;.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-19 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found