#!/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 handle 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 file # # 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, ") { # 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 store 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 which 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 $activePorts[$index] \n"; $handle->close; splice(@handles, $index, 1); my $badPort = $activePorts[$index]; splice(@activePorts, $index, 1); $thread = new Thread \&tryRecoveringBadConnections, "$badPort"; } } else { $handle->send("hi"); # Error over here. Error is: Can't locate object method "send" via package "IO::Socket::INET=GLOB(0x1234gf0)" # perhaps you forgot to load "IO::Socket::INET=GLOB(0x1234gf0)"?) print "Data sent on port $activePorts[$index] \n"; $eventCount++; $index++; } } } } $thread->join;