Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

TCP Client is not working

by Rahul Gupta (Sexton)
on Aug 22, 2013 at 12:52 UTC ( #1050523=perlquestion: print w/ replies, xml ) Need Help??
Rahul Gupta has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have developed code for remote communication with TCP.

Client code is

#!perl -w use strict; use IO::Socket::INET; my $channel_id = open_channel("127.0.0.1", "7777"); send_command($channel_id, "dir"); sub open_channel { # create a connecting socket my $host = shift; my $port = shift; if ( $host !~ m/(\d+)\.(\d+)\.(\d+).(\d+)/i ) { print "IP Address:'$host' is not a Valid IP-Address."; return 139; } my $socket = new IO::Socket::INET ( PeerHost => $host, PeerPort => $port, Proto => 'tcp', ); die "cannot connect to the server $!\n" unless $socket; if ( !defined $socket ) { print "Opening TCP Channel to host: $host and Port: $port Fail +ed !!"; return 138; } else { $socket->send("Check Connectivity"); while (1) { my $recieved_data= <$socket>; if ( defined $recieved_data ) { chomp($recieved_data); if ($recieved_data =~ /Connected to TCP socket server/ +i){ print "TCP Connection established with Channel id: + '$socket'\n"; return $socket; } else{ print "Cannot craete connection with server\n"; return 137; } } } #$socket->close(); } } sub send_command { my $channel_id = shift; my $command_to_send = shift; print "In send_command sub-routine:-Channel_id: '$channel_id' and + command: '$command_to_send'"; $| = 1; chomp($command_to_send); print $channel_id $command_to_send."\n"; while (1) { my $recieved_data = <$channel_id>; if ( defined $recieved_data ) { chomp($recieved_data); print "recieved_data=======>$recieved_data\n"; if ( $recieved_data =~ m/^done/i ) { return 0; } } } #$channel_id->close(); }

server code is:

#!/usr/bin/perl -w use strict; use IO::Socket::INET; my $Receiving_Port = 7777; # flush after every write $| = 1; my ( $socket, $received_data ); my ( $peeraddress, $peerport ); $socket = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => $Receiving_Port, Proto => 'tcp', Listen => 5, Reuse => 1 ) or die "ERROR in Socket Creation : $!\n"; $socket->listen(); $socket->autoflush(1); print "TCP Server Started\n"; my $addr; while (1) { $addr = $socket->accept(); print "$addr\n"; $peeraddress = $addr->peerhost(); $peerport = $addr->peerport() ; if ( $^O !~ m/mswin32/i ) { $SIG{CHLD} = 'IGNORE'; } while (<$addr>) { # Read all messages from client # Print received message $received_data = $_; chomp($received_data); my $exitCode = RunCommand( $received_data, $peeraddress, $peer +port ); #exit $exitCode; } } $socket->close(); sub RunCommand { my $Command_To_Send = shift; my $send_ip = shift; my $peerport = shift; # file handler which will have response values. my $fileHandler; print "Executing: '$Command_To_Send'\n"; print "Output on: '$send_ip:$peerport'\n"; if ( $Command_To_Send =~ m/Check Connectivity/i ) { print $addr "Connected to TCP socket server\n"; return 0; } if ( $Command_To_Send =~ m/\x03|\x1A/i ) { return 0; } # and assign it to fileHandler. my @pid = open( $fileHandler, '-|', "$Command_To_Send 2>&1" ); if ( $^O !~ m/mswin32/i ) { #Traversing pid array. foreach (@pid) { print "'$Command_To_Send' PID: $_\n"; print $addr "Process ID = $_\n"; } } if ( $#pid == -1 ) { warn "Cannot Open Command !!\n"; print $addr "'$Command_To_Send' Unable to Start\n"; } my $ofh = select $fileHandler; $| = 1; select $ofh; print $addr "Sending Command '$Command_To_Send'\n"; while (<$fileHandler>) { print $addr $_; } print $addr "\n"; print $addr "normal-exit\n"; print $addr "done\n"; # Traversing pid array. foreach (@pid) { my $curr_pid = $_; if ( $^O !~ m/mswin32/i ) { kill 9, $curr_pid; } } close($fileHandler); return 0; }

Whenever i am executing the code ,it stucks at this point:

TCP Connection established with Channel id: 'IO::Socket::INET=GLOB(0x3 +494d8c)' In send_command sub-routine:-Channel_id: 'IO::Socket::INET=GLOB(0x349 +4d8c)' and command: 'dir'

Can anyone help me to resolve this issue.

Thanks in advance

Comment on TCP Client is not working
Select or Download Code
Re: TCP Client is not working
by golux (Pilgrim) on Aug 22, 2013 at 13:52 UTC
    Hi Rahul Gupta,

    When you write to the socket from the client, be sure to put a newline at the end:

    $socket->send("Check Connectivity\n");

    That makes the program seem to work for me, all the way to:

    recieved_data=======>Sending Command 'dir' recieved_data=======>client.pl server.pl recieved_data=======> recieved_data=======>normal-exit recieved_data=======>done

    which it wasn't getting to before.

    say  substr+lc crypt(qw $i3 SI$),4,5

      Thanks for you reply.

      It worked for me :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050523]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (17)
As of 2014-10-01 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (35 votes), past polls