简体   繁体   中英

Perl: simultaneous socket connection

Can I connect to array of hosts simultaneously? When I try Socket

use Socket;
socket($s, AF_INET, SOCK_STREAM, getprotobyname('tcp'));
my $addr = sockaddr_in(80, inet_aton("192.168.1.1"));
connect($s, $addr);

or IO::Socket

use IO::Socket;
my $s = IO::Socket::INET->new(PeerAddr => $ip , PeerPort => 80 , Proto => 'tcp' , Timeout => 5);

Is there any other way except forking and threading to avoid waiting on establishing connection? The final expected result is array of opened socket handles.

If you want to go low-level, you can use the built-in select() function to work out which sockets are readable/writable. There's also the IO::Socket that adds a more convenient API.

But a better answer is to use one of the event modules like AnyEvent .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM