develooper Front page | perl.beginners | Postings from January 2002

RE: Computer connected to the internet?

Thread Previous | Thread Next
From:
Gary Hawkins
Date:
January 24, 2002 03:40
Subject:
RE: Computer connected to the internet?
Message ID:
LOBBIJFNBAGIOILLFAIBOEJBNFAA.ghawk@eskimo.com
> >How can I check whether the system is connected?
> >
> >Want to pause the script if connection is lost.
>
<snip>
>
> Instead of trying to check whether "there is an Internet connection," just
> see whether you can do whatever it is your program is using the Internet
> for.
>
<snip>
>
> Peter Scott
> peter@psdt.com
> http://www.perldebugged.com
>

I like that suggestion.  So, how can I determine it based on the success of
this second line?:

&CheckOnlineState;
$request  = HTTP::Request->new(HEAD => "$URL");

In the meantime, I came up with the following Win32::API read of a dll
function, which works.  Supposing that the $request success test above winds up
in a catch 22 without a way to recover and retry, I would like to find some
sort of associated method (without ping) to check online state if on Linux.
Here's the one for Windows:

if ($^O eq "MSWin32") { use Win32::API; } # top of script
# The 'if' is because, for some ungodly reason my Linux-based
# ISP doesn't have Win32::API installed, imagine that.

sub CheckOnlineState {                    # elsewhere
	return if $^O ne "MSWin32";
	$IsNetworkAlive = Win32::API->new(
            "SENSAPI.DLL", "IsNetworkAlive", [P], "L");
	$return_code = "\0" x 4; # reserve some memory for it, why?
	$IsNetworkAlive->Call( $return_code );
            # or warn Win32::FormatMessage(Win32::GetLastError());
	$return_code = unpack('L',$return_code); # hash otherwise?
	#print "\nReturn code: $return_code\n";
	if ($return_code == 0) {
		print "\n\n\tConnection lost!
                       \tRestore and then
                   press any key to continue...\n\n";
		`pause`;
	}
	# From C?:  Declare Function IsNetworkAlive \
      #            Lib "SENSAPI.DLL" \
	#            (ByRef lpdwFlags As Long) As Long
	# lpdw apparently = long pointer double word, so, [P].
	# ... IsNetworkAlive As Long, so, "L", right?
}

That allows the script to pick up where it left off when my 2-hr-limit DSL
connection times out and I reconnect it via the any_key.

One small problem with the routine is that I see it return 2 for my particular
connection type if connected but there is supposed to be some associated
descriptive text along with that, differentiating between LAN or WAN or
something, and I don't know which is which for 1 or 2 or how to get that text.
Oh well, zero works.  Thanks to those who responded, all comments were
appreciated and helpful.

That's the furthest I've gotten with Win32::API.  Now it's tempting to go
bonkers looking up functions in dll's and exe's via notepad or QuickView and
run them from Perl to amuse myself.  Or maybe it has a greater use, maybe girls
would just about faint when they hear this pickup line:  "I access Windows
API's from Perl all the time, yeah I have complete control of Windows."  :b
....have to give that a try at Starbuck's first thing in the morning.  Hey I'm
in Seattle and the traffic has driven us all mad so gimme a break.  :/

Gary







Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About