Thanks for the reply.
Anyways here is my client side code.
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use IO::Select;
use IO::Socket::INET;
use Thread;
my @handles;
my @activePorts;
my @events;
sub PortConn {
my ($port)=@_;
print " Trying to connect to port $port .... \n";
my $rethandle ;
my $retryCount=0;
LOOP: until ($rethandle = new IO::Socket::INET (PeerAddr => 'x.
+x.x.x', PeerPort => $port, Proto => 'tcp', )){
sleep 1; # changed ip to x.x.x.x
last LOOP if (++$retryCount > 10); # If not able to connect in
+ 10 seconds then break out of loop
}
if ($retryCount > 10) { # Cannot connect to port since retryCount
+ is greater than 0. Therefore putting in bad file
print "Could not connect to port $port \n";
open FILE, ">> ./badFile" or die "Error: $!";
print FILE "$port \n";
close(FILE);
return;
}
else { # Connected to specified port therefore putting file handl
+e and port in good file
$rethandle->autoflush(1);
print("Connected to port $port \n");
open FILE, ">> ./goodFile" or die "Error: $!"; # open file for
+ append, die otherwise
print FILE "$rethandle $port \n";
close(FILE);
return;
}
}
######################################################################
+####
# Function to store all the active ports and handles from goodFile fil
+e #
# into appropriate arrays for active ports and handles
+ #
######################################################################
+####
sub ActivePortsAndHandles {
if (-s "goodFile") { # Check if file exist and is non-zero size
splice(@handles); # Removing all elements from array. This
+ way we will only store active handles in them.
splice(@activePorts); # Removing all elements from array. This way
+ we will only store active ports in them.
open FILE, "<goodFile";
while (<FILE>) { # looping through all the lines in the file, one
+ at a time
my @array=split(/ /,$_); # since entry in the file will be in
+the format
# filehandle port. Therefore splitting it to sto
+re all the
# file handles and ports in separate arrays
push(@handles, $array[0]);
push(@activePorts , $array[1]);
}
close(FILE);
}
}
###############################################
# Function to try and recover bad connections #
# If the connection is recovered it will be #
# removed from the bad file and will be put #
# under good file #
###############################################
sub tryRecoveringBadConnections {
my ($badPort) = @_;
my $removeFromGoodFile="sed \'\/$badPort\/d\' goodFile > tmp &
+& mv tmp goodFile";
print $removeFromGoodFile;
open FILE, ">>badFile";
print FILE "$badPort \n";
close(FILE);
}
#############
# Main Code #
#############
my @ports = (33333, 33334, 33335);
foreach my $port (@ports) {
PortConn($port);
}
ActivePortsAndHandles();
for (my $index=1; $index<101; $index++) { # function to set events whi
+ch we will be sending across the socket
push(@events, "event$index");
}
my $eventCount=0;
my $receiveText;
my $index=0;
my $thread;
while($eventCount != scalar(@events)) {
while (scalar(@handles)) {
$index=0;
foreach my $handle (@handles) {
my $select = IO::Select->new();
$select->add($handle);
if ($select->can_read(0.5)) {
print "Can read \n";
$handle->recv($receiveText, 128);
if ($receiveText eq '') {
print "Lost connection to server on port $activePo
+rts[$index] \n";
$handle->close;
splice(@handles, $index, 1);
my $badPort = $activePorts[$index];
splice(@activePorts, $index, 1);
$thread = new Thread \&tryRecoveringBadCon
+nections, "$badPort";
}
}
else {
$handle->send("hi"); # Error over here. Error is: Can'
+t locate object method "send" via package "IO::Socket::INET=GLOB(0x12
+34gf0)"
# perhaps you forgot to load "IO:
+:Socket::INET=GLOB(0x1234gf0)"?)
print "Data sent on port $activePorts[$index] \n";
$eventCount++;
$index++;
}
}
}
}
$thread->join;
Server side code just prints the message received from client.
Thanks.
-
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.