Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by Anonymous Monk
on Dec 02, 2012 at 22:30 UTC ( #1006761=note: print w/ replies, xml ) Need Help??


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

I don't know :) seems to me you're passing a socket when you spawn/create a thread, no sharing required

 [thread socket] thread socket -> threads + sockets stalling question, Multithreaded Socket Listener -> Simple threaded chat server


Comment on Re: Pass a IO::Socket handle to a thread
Download Code
Re^2: Pass a IO::Socket handle to a thread
by bsdmeister (Initiate) on Dec 02, 2012 at 22:53 UTC

    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); }

      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 ++.
      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

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-08-28 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (263 votes), past polls