Perl: How to get IO::Socket::INET timeout after X seconds?

前端 未结 2 1633
梦谈多话
梦谈多话 2021-01-02 17:57

I\'m trying to connect to some host, using invalid port, and i want to get timeout after X seconds. How to do that ?

My code:

 $sock = new IO::Socket         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 18:38

    So much easier is to use the IO::Socket::Timeout

    as per below and it works like a charm.

    use IO::Socket::Timeout;
    my $socket = IO::Socket::INET->new( Timeout => 2 );
    IO::Socket::Timeout->enable_timeouts_on($socket);
    $socket->read_timeout(0.5);    # These will work
    $socket->write_timeout(0.5);   # These will work
    

提交回复
热议问题