Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

by bsdmeister (Initiate)
on Dec 02, 2012 at 22:53 UTC ( #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
    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.


    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006762]
[usemodperl]: did we get invaded by soy boys or what? it wasn't like this 20 years ago :-)
[usemodperl]: i can see the old tgimers hiding out on reddit! lol
[usemodperl]: no wonder this place is so... popular
[marto]: your account is 7 days old, how old are these 'old tgimers'?
[choroba]: usemodpoerl Do you have a question? Do you want to help someone? Or do you just want to troll?
[usemodperl]: perlmonks used to be a safe space for perl coders, now it's like having an annoying girlfriend...
[marto]: I had a feeling I'd regret looking up the definition of 'soyboy'
[Veltro]: usemodperl Maybe it is the way you talk? 'Soy boys', 'old ladies' and what is even a tgimer...
usemodperl is venting, duh?
[usemodperl]: that was a typos, dumbass

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-06-24 15:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.