#!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 Failed !!"; 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(); }