Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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]
[jdporter]: one prog I have has a UI which allows to input a bit mask
[jdporter]: currently it expects the bitmask to be in the form of a hex number, e.g. 0x0101
[jdporter]: but I'd like to let the user specify it as individual bits, i.e. 0b0000000100000001
[jdporter]: there is no bit/binary equivalent of hex, right?
[jdporter]: I guess that the most direct way of doing it is with some magical incantation involving unpack or whatever
[jdporter]: let me google that for me ;-)
[jdporter]: hex points explicitly to oct, which does the job. :-D
[jdporter]: omg, I f love Perl!
[choroba]: say unpack 'H*', pack 'B*', $mask =~ /0b([01]+)/;

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-02-20 16:30 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (272 votes). Check out past polls.