I am using IO::Scoket module with tcp protocol.
server is running on linux box:
server code:
#!/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.
if ( $Command_To_Send =~ m/Check Connectivity/i )
{
print $addr "Connected to TCP socket server\n";
return 0;
}
return 0;
}
client is running on windows machine.
Client code
#!perl -w
use strict;
use IO::Socket::INET;
my $channel_id = open_channel("127.0.0.1", "7777");
sub open_channel {
# create a connecting socket
my $host = shift;
my $port = shift;
my $alrm_time_out = 5;
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 {
local $SIG{ALRM} = sub {
close $ChannelID;
print "=> Time-Out as no data received for '$alrm_time_out
+ sec'.!!\n";
};
$socket->send("Check Connectivity");
alarm $alrm_time_out;
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;
alarm 0;
}
else{
print "Cannot craete connection with server\n";
alarm 0;
return 137;
}
}
}
#$socket->close();
}
}
Problem: when server doesn't respond in that case alarm should invoke at client side but things are not happening so. please help me how to kill the client process when server doesn't respond.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.