Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Socket Handle not sending data ?

by targetsmart (Curate)
on Jul 09, 2009 at 13:09 UTC ( [id://778562]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Socket Handle not sending data ?
in thread Socket Handle not sending data ?

Lauching a thread everytime to check the availability of the socket connection is not a good idea.
just check defined on socket handle or check eof on socket handle or establish a heart beat mechanism to know the liveliness of the socket/program.
If you show the piece of code which requires efficiency, we would able to tell you what could be the best way of dealing it.


Vivek
-- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.

Replies are listed 'Best First'.
Re^4: Socket Handle not sending data ?
by evilkamikaze (Initiate) on Jul 09, 2009 at 14:19 UTC

    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 &amp; +&amp; 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 \&amp;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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://778562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found