Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there anyway i can check my internet connection (on a win32 system) without using any clunky modules?

Replies are listed 'Best First'.
Re: Check an internet connection?
by Cine (Friar) on Aug 20, 2001 at 10:46 UTC
    The easy one:
    ping <some website you are sure running>

    TIMTOWTDI

      All credit to Cine

      C:\>ping www.perlmonks.com Pinging www.perlmonks.com [206.170.14.76] with 32 bytes of data: Reply from 206.170.14.76: bytes=32 time=430ms TTL=237 Reply from 206.170.14.76: bytes=32 time=333ms TTL=237 Reply from 206.170.14.76: bytes=32 time=360ms TTL=237 Reply from 206.170.14.76: bytes=32 time=330ms TTL=237 C:\>ping www.i_don't_exist.com Bad IP address www.i_don't_exist.com. C:\>ping Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS][-r count] [-s count] [[-j host-list] | [-k host-list]] [-w timeout] destination-list Options: -t Ping the specifed host until interrupted. -a Resolve addresses to hostnames. -n count Number of echo requests to send. -l size Send buffer size. -f Set Don't Fragment flag in packet. -i TTL Time To Live. -v TOS Type Of Service. -r count Record route for count hops. -s count Timestamp for count hops. -j host-list Loose source route along host-list. -k host-list Strict source route along host-list. -w timeout Timeout in milliseconds to wait for each reply. C:\>

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Check an internet connection?
by idnopheq (Chaplain) on Aug 20, 2001 at 15:27 UTC
(crazyinsomniac) Re: Check an internet connection?
by crazyinsomniac (Prior) on Aug 20, 2001 at 12:08 UTC
Re: Check an internet connection?
by RatArsed (Monk) on Aug 20, 2001 at 12:14 UTC
    The wininet API (which is updated by installing newer versions of Internet Explorer) has functions to do exactly this.

    Unfortunatly for you, I've not got any example code of using it from Perl, but it may be worth looking at the relevent Microsoft newsgroup for help with WinInet

    Update: I forgot to mention this API uses the same setting that IE uses, and can give false results. However, you can also use other functions from the same API to connect to the Internet using the client's configured proxy settings, etc. (13:20, 20-Aug)

    --
    RatArsed

Re: Check an internet connection?
by htoug (Deacon) on Sep 13, 2001 at 13:53 UTC
    How about using Net::Ping?
    use Net::Ping; $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close();
    It's part of the normal ActiveState distribution.
Re: Check an internet connection?
by Anonymous Monk on Aug 20, 2001 at 11:55 UTC
    mayhaps i should specify....how do i check my connection from inside perl? thankus everyone
      Ok.
      #!/usr/bin/perl -w use strict; use Carp; my $host = "www.perlmonks.com"; my $timeout = 10; #sec #Stolen from Net::Ping use FileHandle; use Socket qw( SOCK_DGRAM SOCK_STREAM SOCK_RAW PF_INET inet_aton sockaddr_in ); my $proto_num = (getprotobyname('tcp'))[2] || croak("Can't get tcp protocol by name"); my $port_num = (getservbyname('echo', 'tcp'))[2] || croak("Can't get tcp echo port by name"); my $fh = FileHandle->new(); my $ip = inet_aton($host) || croak("Cant resolve: $!"); socket($fh, &PF_INET(), &SOCK_STREAM(), $proto_num) || croak("tcp socket error - $!"); my $saddr = sockaddr_in($port_num, $ip); $SIG{'ALRM'} = sub { die }; alarm($timeout); # Interrupt connect() if we have to my $ret = 0; # Default to unreachable eval <<'EOM' ; exit unless connect($fh, $saddr); $ret = 1; EOM alarm(0); $fh->close(); exit $ret;


      TIMTOWTDI