Jump to content

Are IPs (or URLs) connected UDF (without slowing the script)


martin
 Share

Recommended Posts

UDF which checks the connected state of up to 50 IPs in the background so that the main script is not slowed.

For each IP an ON and an OFF icon can be specified. The update frequency can be adjusted, IPs added or removed and the icons can be changed at any time.

When a new address is added to the list it can given given a group number. This allows easy removal of IPs by specifying that you want to remove all IPs in a certain group.

A user function can be called (V2.3 on) every period with the state of the IP connections.

The function is only called if the state changes or changes to stable where stable means two consecutive tests gave the same result.

The download has the UDF, an example for setting up to 5 IPs, NomadMemory.au3 which is used by the udf, and Prog@ndy's "Read_ICLFormat.au3" because I used that in the example.

Functions

_IsConnectedStartUp

_IsConnectedAddIP

_IsConnectedRemoveIP

_IsConnectedRemoveIcon

_IsConnectedReset

_IsConnectedSetFreq

_IsConnectedVersion

_IsConEvent

_IsConnectedGetLastEntry

Download from here.

EDITS:

31st Jan 2009 V2.4

Added _IsConnectedGetLastEntry to find the last entry number in the list.

Added _IsConnectedRemoveIP which removes all entries of a given IP

Added a parameter to _IsConnectedReset so you can clear from a given item onwards rather than clear all.

30th Jan 2009 V2.3 Added _IsConnectedEvent function to call a user defined function when a connected state changes. Example updated.

27th Jan 2009 Added nomadmemory.au3 to the download, V2.2 corrected error in removing icon if the last in the list, small improvements to example.

25th Jan 2009 V2.1 updated example to use the _IsConnectedRemoveIcon function, and changed that function to ensure icon shown in OFF state.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • 2 weeks later...

Thx for sharing. I wrote a similar func a while back but not entirely reliable though...

Regards,

ivan

The udf I've shown uses a message from the foreground to the background program when an icon needs to be updated or an ip address is changed. If the background is executing ping when the message is sent it delays the message handling, and if there are a lot of icon or ip changes then the delays means the udf isn't doing what it is supposed to do. I am rewritting the udf to used shared memory to get round this problem, and when it's done I'll change my first post. Meanwhile what I posted is fine if things aren't changed often.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I found this code somewhere in this forum, and it is supposed to check for internet connection.

What I found is that it checks for LAN connection and not Internet.

Anybody can shed some light on this?

CODE

$IsCon = DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int_ptr", 0, "int", 0)

If $IsCon[0] = 0 Then

$connected = 0

Else

$connected = 1

EndIf

Return $connected

Link to comment
Share on other sites

  • Moderators

I found this code somewhere in this forum, and it is supposed to check for internet connection.

What I found is that it checks for LAN connection and not Internet.

Anybody can shed some light on this?

CODE

$IsCon = DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int_ptr", 0, "int", 0)

If $IsCon[0] = 0 Then

$connected = 0

Else

$connected = 1

EndIf

Return $connected

This is a support question that is hijacking this thread. The sad thing is doing a search for "InternetGetConnectedState" would have "shed light".

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I'm still curious why martin used Ping() to do the job. That is my question to martin.

InternetGetConnectedState apperas to be (and is) much better solution.

Really nice and pragmatical other methods in script.

But not needed if InternetGetConnectedState used.

I guess subtitle of this thread is what it's about. That would explain it.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

I'm still curious why martin used Ping() to do the job. That is my question to martin.

InternetGetConnectedState apperas to be (and is) much better solution.

Really nice and pragmatical other methods in script.

But not needed if InternetGetConnectedState used.

I guess subtitle of this thread is what it's about. That would explain it.

Well it doesn't really explain it :)

First of all as far as I can tell, InternetGetConnectedState doesn't actually tell if you have a connection to the internet. msdn says

Retrieves the connected state of the local system.

which is not the same. If I use rasim's script here for example, it will give me the same result whether I unplug my ADSL router or not, and when it's unplugged I definitely have no connection to the internet. (If I've got something wrong there I'd like to be told of course.)

Secondly, the intended purpose of my script is to see if there is access to a particular list of IPs, which can change, and ping seems the easiest way. InternetGetConnectedState can't tell you that.

It is also an example of running another process in parallel and in that respect whether ping is best or not is not important.

I am going to replace the udf when I get the replacement working so that changing any of the IPs won't cause delays in the main script.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Indeed, that would be a drawback to InternetGetConnectedState/InternetGetConnectedStateEx.

InternetCheckConnection would probably be the more appropriate WinINet analogue/alternative for your Ping() usage (it allows you to test against a specific host URL). It's not an asynchronous call, though, so it does still lock script execution if it's having trouble connecting. And thus, it's useless an alternative (at least no more useful than using Ping() :)).

Edit: Perhaps you may be able to play with InternetSetStatusCallback, but I've never tried it, so I don't know for sure how/whether it would work with InternetCheckConnection.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

Well it doesn't really explain it :)

First of all as far as I can tell, InternetGetConnectedState doesn't actually tell if you have a connection to the internet. msdn says

which is not the same. If I use rasim's script here for example, it will give me the same result whether I unplug my ADSL router or not, and when it's unplugged I definitely have no connection to the internet. (If I've got something wrong there I'd like to be told of course.)

Secondly, the intended purpose of my script is to see if there is access to a particular list of IPs, which can change, and ping seems the easiest way. InternetGetConnectedState can't tell you that.

It is also an example of running another process in parallel and in that respect whether ping is best or not is not important.

I am going to replace the udf when I get the replacement working so that changing any of the IPs won't cause delays in the main script.

You are not interpreting MSDN right. The accent should be on: A return value of TRUE from InternetGetConnectedState indicates that at least one connection to the Internet is available.

That is to say that your system is online.

rasim's script is not checking what it should check. Try the following code. If it give you false results then I'm wrong and you are right:

#cs
Global Const $INTERNET_CONNECTION_MODEM = 1
Global Const $INTERNET_CONNECTION_LAN = 2
Global Const $INTERNET_CONNECTION_PROXY = 4
Global Const $INTERNET_CONNECTION_MODEM_BUSY = 8
Global Const $INTERNET_RAS_INSTALLED = 16
Global Const $INTERNET_CONNECTION_OFFLINE = 32
Global Const $INTERNET_CONNECTION_CONFIGURED = 64
#ce

ConsoleWrite("---------------------" & @CRLF)

Local $a_iCall = DllCall("wininet.dll", "int", "InternetGetConnectedState", "dword*", 0, "dword", 0)

If @error Or Not IsArray($a_iCall) Then 
    ConsoleWrite("! DllCall failed" & @CRLF)
    ConsoleWrite("---------------------" & @CRLF)
    Exit
EndIf

If $a_iCall[0] Then 
    ConsoleWrite("! System is online" & @CRLF)
Else
    ConsoleWrite("! System is offline" & @CRLF) 
EndIf

ConsoleWrite("+> Characteristics:" & @CRLF & _ConnectionDescription($a_iCall[1]))

ConsoleWrite("---------------------" & @CRLF)




Func _ConnectionDescription($iFlag)

    If $iFlag < 1 Or $iFlag > 127 Then
        Return SetError(0, 0, "")
    EndIf

    Local $aFlag[7][2] = [[1, " - local system has a valid connection to the Internet, but it might or might not be currently connected"], _
            [2, " - local system can use a local area network to connect to the Internet"], _
            [4, " - local system can use a modem to connect to the Internet"], _
            [8, ""], _
            [16, " - local system has RAS (Remote Access Server) installed"], _
            [32, " - local system is in offline mode"], _
            [64, " - local system can use a proxy server to connect to the Internet"]]

    Local $sOut
    Local $a

    While 1
        $a = Floor(Log($iFlag) / Log(2))
        If $a < 0 Then
            ExitLoop
        EndIf
        $sOut = $sOut & $aFlag[$a][1] & @CRLF
        $iFlag -= $aFlag[$a][0]     
    WEnd

    Return SetError(0, 0, $sOut)

EndFunc ;==>_ConnectionDescription

InternetCheckConnection is pinging.

edit: IsArray() added

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

@trancexx: Your example still shows that InternetGetConnectedState doesn't work as you're expecting. I just disconnected my computer's Ethernet cord (its only connection to the Internet), and the script continued to tell me that I was online. If you have a LAN set up, WinINet seems to assume you have an Internet connection, regardless of whether it's actually active. InternetGetConnectedStateEx tells you the name of the connection WinINet is assuming works. (In my case, it always returns "LAN Connection" in the provided struct, whether or not the Ethernet cord is connected.)

And yes, I know InternetCheckConnection pings. I said it was the WinINet analogue to Ping(), did I not?

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

@trancexx: Your example still shows that InternetGetConnectedState doesn't work as you're expecting. I just disconnected my computer's Ethernet cord (its only connection to the Internet), and the script continued to tell me that I was online. If you have a LAN set up, WinINet seems to assume you have an Internet connection, regardless of whether it's actually active. InternetGetConnectedStateEx tells you the name of the connection WinINet is assuming works. (In my case, it always returns "LAN Connection" in the provided struct, whether or not the Ethernet cord is connected.)

And yes, I know InternetCheckConnection pings. I said it was the WinINet analogue to Ping(), did I not?

Well, I know that you know but I was replying to martin's post. You are martin? Really.

added IsArray() to that script (was wrong before)

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

InternetCheckConnection is pinging.

... martin never mentioned InternetCheckConnection. If you were replying to anyone with that particular line, who else could I have assumed it to be for besides me, the only person to have mentioned that particular function in this thread? But whatever, I really don't want to hijack martin's thread, and I don't want to have a debate over something as silly as this :)

At any rate, there couldn't have been a problem with DllCall() not returning an array, as testing @error immediately after the DllCall() takes care of that. InternetGetConnectedState just doesn't work for what martin intends for this script do.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

... martin never mentioned InternetCheckConnection. If you were replying to anyone, who else could I have assumed it to be for besides me, the only person to have mentioned that particular function in this thread? But whatever.

At any rate, there couldn't have been a problem with DllCall() not returning an array, as testing @error immediately after the DllCall() takes care of that. InternetGetConnectedState just doesn't work for what martin intends for this script do.

You couldn't be more right.

Sorry about that, I was reading another thread that was discussing this and watched game on tv, so... you know.

So, it's sure that return value "False" is certain sign of not being online. Right? If this is true than at least half of the job is done.

To be totally offtopic; I never did understand IsArray() check after DllCall() and never used it but I did put "Not $a_iCall[0]" and just wanted to fix that.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

(The only time DllCall() doesn't return an array is when an error occurrs with DllCall() itself, in which case @error is set to a non-zero value)

Indeed, returning False would definitely be a certain sign that there is no Internet connection, but that's more a sufficiency argument than it is a necessity argument. Meaning it won't be of much help for people wanting to find out whether there is a connection :)

So, to get this thread back on track... Nice work on the example martin, it makes use of an interesting workaround to script delays while waiting for a result to be returned :lmao: (Sorry for the slight detour I may have partly caused)

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

@trancexx

I tried your script in post 11 (after the edit) and as ultima says, it says I'm on line when I'm not.

@ultima thanks for your input. I don't think that you or trancexx are hijacking my thread, I like both your contributions.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I changed the first post so that the udf is a lot different now. It uses memory instead of messages so that the main script won't be slowed down by the IP checks that are going on.

The example has been replaced with a better one.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I changed the first post so that the udf is a lot different now. It uses memory instead of messages so that the main script won't be slowed down by the IP checks that are going on.

The example has been replaced with a better one.

Can you explain in steps how it works now?

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Can you explain in steps how it works now?

Here's a quick description of the way it works. If it doesn't make sense then tell me and I'll do a better job of explaining.

When you run the startup function it creates an array to hold info on the icons used, and runs the udf as a separate script which I'll call udf2. When the udf2 runs it checks to see if the script name being run is IsConnected2.au3 or .exe. If it is then it knows that it is being run as a spearate process and so it calls the run parallel function. This function creates a window so that it can recieve a message. The window is normally hidden but in my example I let you see it if you want. Then udf1, (running startup from the main script) sees the udf2 is running and sends it a messaqge to give udf2 the address of the dllstruct and the PID of the main script which udf2 needs to be able to open udf1's memory.

Now udf2 only needs to read the memory of the struct. When udf1 wants to update information it just writes it to the struct, and if udf2 is held up waiting for a ping to finish it won't matter because it will just read the new data in the struct when it's ready. I hope that the information I put in the script will explain the way the structure is used.

A timer set by udf2 signals when time is up to start checking the ips again. If this timer signals the time before the pingb function has finished the last time it doesn't matter because it will just run pingb again when it has returned to the main idle loop and seen that it has to run pingb again. While it's in the idle loop it changes the timer if udf1 has changed the update value.

It would have been easier in some ways if I made 2 different scripts; one as a udf to be included in the main script and the other to be run as the parallel process. The only reason I didn't was that I thought it was easier to have just one file and, providing it works, most people probably don't worry about what going on inside.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...