 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Tasman
Joined: 20 May 2006 Posts: 21
|
Posted: Sat May 20, 2006 9:21 pm Post subject: Check if connected to the internet or not |
|
|
This script, based mostly on code from the Winlirc script, checks to see if an internet connection is available.
What it does is open and close a socket to the test address (ip number), if sucessfull there must be an open connection.
Looking at the code this might seem to be an eleborate way of doing it but it is actually faster then doing a ping and results in even less overhead in windows then does a simple ping.
As a ping involves opening a socket, sending a packet, waiting for the reply, timing the whole process etc.
And ofcourse it doesn't involve temp files and/or other utilities.
I'm using this script as part of a menu that displays the connection status whenever it is opened.
The script can easily be modified to take an action based on the result.
If you are looking for a script that will take a hostname and convert it to an ip address for use with this script it can be found here: http://www.autohotkey.com/forum/viewtopic.php?t=9937
| Code: |
NodeName = www.google.com
IPs := HostToIp(NodeName)
DllCall("Ws2_32\WSACleanup") ; always inlude this line after calling to release the socket connection
if IPs <> -1
Msgbox, %NodeName%`n%IPs%
else
MsgBox, Host "%NodeName%" not found
HostToIp(NodeName) ; returns -1 if unsuccessfull or a newline seperated list of valid IP addresses
{
VarSetCapacity(wsaData, 32) ; The struct is only about 14 in size, so 32 is conservative.
result := DllCall("Ws2_32\WSAStartup", "UShort", 0x0002, "UInt", &wsaData) ; Request Winsock 2.0 (0x0002)
if ErrorLevel ; check ErrorLevel to see if the OS has Winsock 2.0 available:
{
MsgBox WSAStartup() could not be called due to error %ErrorLevel%. Winsock 2.0 or higher is required.
return -1
}
if result ; Non-zero, which means it failed (most Winsock functions return 0 on success).
{
MsgBox % "WSAStartup() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError") ; %
return -1
}
PtrHostent := DllCall("Ws2_32\gethostbyname", str, Nodename)
if (PtrHostent = 0)
Return -1
VarSetCapacity(hostent,16,0)
DllCall("RtlMoveMemory",UInt,&hostent,UInt,PtrHostent,UInt,16)
h_addr_list := ExtractInteger(hostent,12,false,4)
VarSetCapacity(AddressList,12,0)
DllCall("RtlMoveMemory",UInt,&AddressList,UInt,h_addr_list,UInt,12)
Loop, 3
{
offset := ((A_Index-1)*4)
PtrAddress%A_Index% := ExtractInteger(AddressList,offset,false,4)
If (PtrAddress%A_Index% =0)
break
VarSetCapacity(address%A_Index%,4,0)
DllCall("RtlMoveMemory" ,UInt,&address%A_Index%,UInt,PtrAddress%A_Index%,Uint,4)
i := A_Index
Loop, 4
{
if Straddress%i%
Straddress%i% := Straddress%i% "." ExtractInteger(address%i%,(A_Index-1 ),false,1)
else
Straddress%i% := ExtractInteger(address%i%,(A_Index-1 ),false,1)
}
Straddress0 = %i%
}
loop, %Straddress0% ; put them together and return them
{
_this := Straddress%A_Index%
if _this <>
IPs = %IPs%%_this%
if A_Index = %Straddress0%
break
IPs = %IPs%`n
}
return IPs
}
ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 4)
{
Loop %pSize%
result += *(&pSource+pOffset+A_Index-1) << 8*A_Index-8
Return result
}
|
Last edited by Tasman on Tue May 23, 2006 10:25 pm; edited 1 time in total |
|
| Back to top |
|
 |
robiandi Guest
|
Posted: Sun May 21, 2006 4:57 am Post subject: |
|
|
| @Tasman: Many thanks for your script. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 6264
|
|
| Back to top |
|
 |
Guest
|
Posted: Mon May 22, 2006 2:10 pm Post subject: |
|
|
Cool! Quick and a lot easier.
Must have searched for the wrong keywords, never spotted this one.
Wish I found it before I tried to wrap my brain around the intricacies of socket programming though.
At least I learned somthing.  |
|
| Back to top |
|
 |
Tasman
Joined: 20 May 2006 Posts: 21
|
Posted: Mon May 22, 2006 2:23 pm Post subject: |
|
|
Dahhh
Just realized why I hadn't spotted it, it was only posted today.
Oops wrong again, there was code posted before by others that showed the way.
Thanks Goyyah.
You wouldn't have an easy way to get a hostname from an IP address would you , somebody else asked for that to.
If not I'll give it a try, it's just that it takes me a while as I don't really grasp all of the socket stuff, but with a lot of trial, error, persistance and tips from this forum, I generally manage to come up with something functional. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 6264
|
Posted: Mon May 22, 2006 3:13 pm Post subject: |
|
|
Dear Tasman,
| You wrote: | Cool! Quick and a lot easier.
Must have searched for the wrong keywords, never spotted this one. |
Yes! I too tripped on it accidentally. Thought I should repost it being a very useful utility.
| You wrote: | You wouldn't have an easy way to get a hostname from an IP address would you , |
Not yet! But I am very interested in learning a way..
Regards,  _________________
 |
|
| Back to top |
|
 |
Tasman
Joined: 20 May 2006 Posts: 21
|
Posted: Mon May 22, 2006 4:46 pm Post subject: |
|
|
Hi Goyyah
Just been trying your connection tip.
Doesn't seem to work correctly for me though.
I'm using a pppoe internet connection over the same network card as that handles the LAN.
So I can be connected to the local Lan without having an internet connection open, this seems to confuse things, and the script reports online status if I am connected to the lan but not the internet.
Drats, I'l have to stick with the much larger code above.
Used to be a big fan of oneliners and your posting came close to it.
About a IP to host script, there is a function Ws2_32\gethostbyaddr that can be used (I think) similarly to the Ws2_32\gethostbyname function in the host to IP script http://www.autohotkey.com/forum/viewtopic.php?t=9937, but I'm stuck for time at the moment. Hopefully I'll be able to concentrate on that option sometime in the next few days.[/url] |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 6264
|
Posted: Mon May 22, 2006 5:00 pm Post subject: |
|
|
Dear Tasman,
| You wrote: | | About a IP to host script, there is a function Ws2_32\gethostbyaddr that can be used (I think) similarly to the Ws2_32\gethostbyname function in the host to IP script http://www.autohotkey.com/forum/viewtopic.php?t=9937, but I'm stuck for time at the moment. Hopefully I'll be able to concentrate on that option sometime in the next few days.[/url] |
Awaiting!..
Regards,  _________________
 |
|
| Back to top |
|
 |
Tasman
Joined: 20 May 2006 Posts: 21
|
Posted: Tue May 23, 2006 10:27 pm Post subject: |
|
|
Just edited my original post and changed the original code for a cleaned up version.
Mostly unnessessary stuff removed. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|