Hi, I have made a TCP socket, both a server and a client, and it works just fine, except from the fact that I cant receive more than one input line from the server when I execute a Unix command trough the socket. And when I am trying to send a ps command from the client to the server to count how many proc a certain user are running, the client does not receive the output, like the server does.
This problem is a bit hard to explain, so please se my two scripts for more: *server.pl* #!/usr/bin/perl # Needed pkg use Getopt::Std; use strict "vars"; # Global variables my $VERBOSE = 0; my $DEBUG = 0; ################################################################ # handle flags and # Example: c == "-c", c: == "-c argument" my $opt_string = 'hvdk:p:'; getopts( "$opt_string", \my %opt ) or usage() and exit 1; # exit other than 0 = error!! # print help message if -h is invoked if ( $opt{'h'} ){ usage(); exit 0; } $VERBOSE = 1 if $opt{'v'}; $DEBUG = 1 if $opt{'d'}; my $PORT = $opt{'p'}; if ( not $PORT ) { print "You need to apply a port number"; usage(); exit 1; } # main program content use IO::Socket; $SIG{"CHILD"} = "IGNORE"; # Enable us to not make the children hangout and wait for the parent to die, (children will die) my $sock = new IO::Socket::INET( LocalPort => $PORT, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1 ); if ( $sock ) { print "Waiting for clients...\n"; while ( my $connection = $sock->accept() ){ my $pid = fork(); # how the server knows if the porc is parent (pid = xxx) or child (0) if ( $pid == 0 ) { my $client = $connection->peerhost(); # use $connection instead of $sock to get ipaddr print "Connection to the $client\n"; my $buffer = <$connection>; chomp $buffer; print "Client said: '$buffer'\n"; # sleep 7; print $connection "Message recived: $buffer\n"; close($connection); exit; # server child should die after finish the conversation } else { close($connection); next; } } } else { die "Error: $!\n"; } close($sock); verbose("verbose\n"); debug("noe\n"); ########################################## # Helper routines sub usage { # print the correct use of this script print "Usage:\n"; print "-h Usage\n"; print "-v Verbose\n"; print "-d Debug\n"; } sub verbose { print $_[0] if ( $VERBOSE or $DEBUG ); } sub debug { print $_[0] if ( $DEBUG ); } *client.pl * #!/usr/bin/perl # Needed pkg use Getopt::Std; use strict "vars"; # Global variables my $VERBOSE = 0; my $DEBUG = 0; ################################################################ # handle flags and # Example: c == "-c", c: == "-c argument" my $opt_string = 'hvdk:p:H:c:'; getopts( "$opt_string", \my %opt ) or usage() and exit 1; # exit other than 0 = error!! # print help message if -h is invoked if ( $opt{'h'} ){ usage(); exit 0; } $VERBOSE = 1 if $opt{'v'}; $DEBUG = 1 if $opt{'d'}; my $PORT = $opt{'p'}; my $SERVER = $opt{'H'}; my $COMMAND = $opt{'c'}; if ( not ( $PORT and $SERVER )){ print "You need to supply a port number and hostname\n"; usage(); exit 1; } # main program content use IO::Socket; my $connection = new IO::Socket::INET( PeerAddr => $SERVER, PeerPort => $PORT, Proto => 'tcp' ) or die "Failed to connect: $!\n"; if ( $connection ) { print "Connection etablished...\n"; # print $connection "hello world!\n"; # print $connection "Isabelle says hello\n"; my $load = `uptime | awk '{print \$8 " " \$9 " " \$10 " " \$11 " " \$12}'`; my $uptime = `uptime | awk '{print \$2 " " \$3 " " \$4}'`; my $mem_tot = `cat /proc/meminfo | grep MemTotal`; if ($COMMAND =~ m/load/){ print $connection $load; } if ($COMMAND =~ m/uptime/){ print $connection $uptime; } if ($COMMAND =~ m/memory/){ print $connection "$mem_tot\n"; } if ($COMMAND =~ m/user:/){ while ($COMMAND){ my @array = split(":", $COMMAND); my $user = $array[1]; my $ps = `ps aux | grep $user | wc -l`; print $connection $ps; sleep 7; } } # $mem_free\n$mem_buff\nswa_tot\n$swa_free\n"; # print $connection `$COMMAND`; my $response = <$connection>; print "Server replied: '$response'\n"; close($connection); } ########################################## # Helper routines sub usage { # print the correct use of this script print "Usage:\n"; print "-h Usage\n"; print "-v Verbose\n"; print "-d Debug\n"; print "-p Port\n"; print "-H Host/Server\n"; } sub verbose { $_[0] if ( $VERBOSE or $DEBUG ); } sub debug { print $_[0] if ( $DEBUG ); }