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

Re^2: Pass a IO::Socket handle to a thread

by bsdmeister (Initiate)
on Dec 02, 2012 at 22:53 UTC ( [id://1006762]=note: print w/replies, xml ) Need Help??


in reply to Re: Pass a IO::Socket handle to a thread
in thread Pass a IO::Socket handle to a thread

I am passing full code here, I changed it thousands of times without success, my goal is quite simple, just read data from the socket inside the thread

#!/usr/bin/perl use Getopt::Long; use IO::Socket::INET; use threads; use threads::shared; #------------------------------------------------- # BEGIN: Variables Declaration #------------------------------------------------- my @replicas; #------------------------------------------------- # END: Variables Declaration #------------------------------------------------- #------------------------------------------------- # BEGIN: Command Line Processing #------------------------------------------------- $parameters = GetOptions("leader=s" => \$type, "malicious=s" => \$state); #------------------------------------------------- # END: Command Line Processing #------------------------------------------------- #------------------------------------------------- # BEGIN: Socket Server Startup and Checks #------------------------------------------------- $connected = 0; if ($type == "TRUE") { $port = 9000; } my $server; while ($connected == 0) { $server = IO::Socket::INET->new( LocalAddr=>'localhost', LocalPort=>$port, Type=>SOCK_STREAM, Proto=>'tcp', Listen=>10 ); if ($server) { print("SERVER: BIND to PORT $port SUCCESFULL\n"); $connected = 1; #if ($type != "TRUE") #{ } else { print("SERVER: CANNOT BIND to PORT $port \n"); $port++; print("SERVER: NEW PORT $port \n"); } } $replica = "localhost:$port"; push(@replicas, $replica); #------------------------------------------------- # END: Socket Server Startup and Checks #------------------------------------------------- #------------------------------------------------- # BEGIN: ServerControl Thread #------------------------------------------------- sub serverControl($time) { while (1) { print "LISTENING \n"; print @replicas; foreach $replica ($replicas) { print $replica; if ($replica != "localhost:$port") { @repl = split(/:/,$replica); print $repl; $clientServerControl = IO::Socket::INET->new( PeerAddr=>'localhost', PeerPort=>$repl[1], Proto =>'tcp' ); if ($clientServerControl) { $keepAlive = "UP:localhost:$replica[1]:ENDUP"; $clientServerControl->send($keepAlive); $answer = <$socket>; print $answer; } else { @replicas = grep { $_ != $replica } @replicas; } close($clientServerControl); } } sleep(2); } } #------------------------------------------------- # END: ServerControl Thread #------------------------------------------------- #------------------------------------------------- # BEGIN: Protocol Thread #------------------------------------------------- sub protocol($srv) { while(1) { # if ($srv->connected()) # { print "Socket is here :)"; $srv->recv($data,1024); @recvData = split(/:/,$data); if ($data) { print "-----> $data"; } print $recvData; # } } } print "aqui"; $t = threads->create('serverControl'); print $type; if ($type eq "FALSE") { $notifyLeader = IO::Socket::INET->new( PeerAddr=>'localhost', PeerPort=>9000, Proto =>'tcp' ) or die("LEADER is Offline"); $notify = "ENTER-VIEW:localhost:$port"; print $notify; if ($notifyLeader->send($notify)) { print "Mensagem enviada... \n"; } close($notifyLeader); } while(1) { print "UP\n"; $client = $server->accept(); $p = threads->create(\&protocol,$client); }

Replies are listed 'Best First'.
Re^3: Pass a IO::Socket handle to a thread
by CountZero (Bishop) on Dec 02, 2012 at 23:08 UTC
    Always
    use strict; use warnings;
    It will avoid silly typos and assist you to find other bugs as well.

    Hint 1: check your if tests. There is a big difference between == and eq.

    Hint 2: forget the subroutine prototypes. They don't do what you think they do.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re^3: Pass a IO::Socket handle to a thread
by Anonymous Monk on Dec 02, 2012 at 23:12 UTC

    Also, you should read perlintro and use eq to compare strings

Re^3: Pass a IO::Socket handle to a thread
by Anonymous Monk on Dec 02, 2012 at 22:59 UTC

    That doesn't compile

    $ perl -Mdiagnostics -e " sub serverControl($time) { } " Illegal character in prototype for main::serverControl : $time at -e l +ine 1 (#1) (W illegalproto) An illegal character was found in a prototype dec +laration. Legal characters in prototypes are $, @, %, *, ;, [, ], &, \, and ++.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found