Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Issue with time() in loop?

by pg (Canon)
on Nov 13, 2005 at 19:00 UTC ( #508128=note: print w/ replies, xml ) Need Help??


in reply to Issue with time() in loop?

Other than what others have already pointed out, I doubt that you need thread. My guess is that the reason you use thread, is to monitor multiple sockets at the same time. A better idea is to use IO::Select.

The following code demoes this. Just run one instance of the server and two instances of the client. Observe how the server receives messages from both clients without threading.

Server:

use strict; use warnings; use IO::Select; use IO::Socket::INET; my $server = IO::Socket::INET->new(Proto => "tcp", LocalAddr => "local +host", LocalPort => 3000, Listen => 10); my $selector = IO::Select->new(); { my $connection = $server->accept(); print "First client connected\n"; $selector->add($connection); } { my $connection = $server->accept(); print "Second client connected\n"; $selector->add($connection); } while (1) { for my $reader ($selector->can_read()) { my $line = <$reader>; print $line; } }

Client:

use strict; use warnings; use IO::Socket::INET; use IO::Select; my $connection = IO::Socket::INET->new(PeerAddr => "localhost", PeerPo +rt => 3000, Proto => "tcp"); for (1 .. 100) { print $connection "$_\n"; sleep(1); }


Comment on Re: Issue with time() in loop?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (18)
As of 2015-07-29 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls