- 论坛徽章:
- 0
|
- #!/bin/env perl
- use strict;
- use warnings;
- use Socket;
- use Fcntl;
- use IO::Select;
- my $socket = &socket_init("192.168.2.3", 80, 5);
- sub socket_init
- {
- my ($host, $port, $timeout) = @_;
- my $socket;
- my $dest;
- if (socket($socket, PF_INET, SOCK_STREAM, 6))
- {
- if ($^O eq 'MSWin32')
- {
- my $nonblocking = pack('L', 1);
- ioctl($socket, 0x8004667e, unpack('I', pack('P', $nonblocking)));
- }
- else
- {
- fcntl($socket, F_SETFL, fcntl($socket, F_GETFL, 0) | O_NONBLOCK);
- }
- my $dest = sockaddr_in($port, inet_aton($host));
- connect($socket, $dest);
- my $select = new IO::Select($socket);
- my ($read_set, $write_set, $error_set) = IO::Select->select(undef, $select, $select, $timeout);
- if ($#$write_set > -1 and $#$error_set == -1)
- {
- ;
- }
- else
- {
- warn "$^E\n";
- $socket = undef;
- }
- }
- return $socket;
- }
复制代码 |
|