Author Topic: Is Internet Connection Available?  (Read 10151 times)

0 Members and 1 Guest are viewing this topic.

AARYAN

  • Newt
  • Posts: 72
Is Internet Connection Available?
« on: April 17, 2013, 09:09:59 AM »
Hi All,

Please help.

Is there a way to check if my computer is connected to internet or not through VisualLisp.
Need a Code to Check and return True if yes and nil if No.

Thanks and Best Regards
Aaryan

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Is Internet Connection Available?
« Reply #1 on: April 17, 2013, 09:46:32 AM »
Maybe something along the lines of:

Code: [Select]
;; Internet-p  -  Lee Mac
;; Returns T if internet connection is available

(defun LM:internet-p ( / obj rtn url )
    (setq url "http://www.google.com/") ;; url to test
    (if (setq obj (vlax-get-or-create-object "winhttp.winhttprequest.5.1"))
        (progn
            (setq rtn
                (vl-catch-all-apply
                    (function
                        (lambda ( )
                            (vlax-invoke obj 'open "GET" url :vlax-false)
                            (vlax-invoke obj 'send)
                            (command "_.delay" 200) ;; Increase if slow connection
                            (vlax-get obj 'status)
                        )
                    )
                )
            )
            (vlax-release-object obj)
            (if (vl-catch-all-error-p rtn)
                (prompt (vl-catch-all-error-message rtn))
                (= 200 rtn)
            )
        )
    )
)
« Last Edit: April 17, 2013, 09:52:12 AM by Lee Mac »

AARYAN

  • Newt
  • Posts: 72
Re: Is Internet Connection Available?
« Reply #2 on: April 17, 2013, 09:52:57 AM »
Thanks You Lee Mac.

But it returns nil and my computer's connection is proper.
What would be the reason?

Thanks Again.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Is Internet Connection Available?
« Reply #3 on: April 17, 2013, 09:54:43 AM »
But it returns nil and my computer's connection is proper.
What would be the reason?

Any error messages?

AARYAN

  • Newt
  • Posts: 72
Re: Is Internet Connection Available?
« Reply #4 on: April 17, 2013, 10:01:24 AM »
Yes There is

WinHttp.WinHttpRequest: The data necessary to complete this operation is not yet available.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Is Internet Connection Available?
« Reply #5 on: April 17, 2013, 10:02:33 AM »
Yes There is

WinHttp.WinHttpRequest: The data necessary to complete this operation is not yet available.

Try increasing the delay noted in the code.

Gasty

  • Newt
  • Posts: 90
Re: Is Internet Connection Available?
« Reply #6 on: April 17, 2013, 10:05:17 AM »
Hi,

Try this:

(defun ping (target)
(= 0 (acet-sys-spawn 3 "ping" target))
)

Test: (ping "8.8.8.8")

Gaston Nunez


AARYAN

  • Newt
  • Posts: 72
Re: Is Internet Connection Available?
« Reply #7 on: April 17, 2013, 10:13:35 AM »
Yes There is

WinHttp.WinHttpRequest: The data necessary to complete this operation is not yet available.

Try increasing the delay noted in the code.

Thats It.. I tried it with 250 and works perfectly.
Thank You So Much Lee Mac.
Is increasing Delay related to the speed of the Internet Connection.

AARYAN

  • Newt
  • Posts: 72
Re: Is Internet Connection Available?
« Reply #8 on: April 17, 2013, 10:14:25 AM »
Hi,

Try this:

(defun ping (target)
(= 0 (acet-sys-spawn 3 "ping" target))
)

Test: (ping "8.8.8.8")

Gaston Nunez

This works perfectly too.
Thanks You So Much Gaston Nunez.

Regards

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Is Internet Connection Available?
« Reply #9 on: April 17, 2013, 10:15:18 AM »
Is increasing Delay related to the speed of the Internet Connection.

Yes; Gaston's solution may be better however - very good Gaston.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Is Internet Connection Available?
« Reply #10 on: April 17, 2013, 10:15:34 AM »
What about this:

Code: [Select]
(defun _ping (address / out ws)
  (if (setq ws (vlax-get-or-create-object "WScript.Shell"))
    (progn (setq out (vlax-invoke ws 'run (strcat "ping.exe -n 1 " address) 0 :vlax-true))
   (and ws (vlax-release-object ws))
   (zerop out)
    )
  )
)
(_ping "google.com")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

AARYAN

  • Newt
  • Posts: 72
Re: Is Internet Connection Available?
« Reply #11 on: April 17, 2013, 10:30:08 AM »
What about this:

Code: [Select]
(defun _ping (address / out ws)
  (if (setq ws (vlax-get-or-create-object "WScript.Shell"))
    (progn (setq out (vlax-invoke ws 'run (strcat "ping.exe -n 1 " address) 0 :vlax-true))
   (and ws (vlax-release-object ws))
   (zerop out)
    )
  )
)
(_ping "google.com")

Thank You So Much!


AARYAN

  • Newt
  • Posts: 72
Re: Is Internet Connection Available?
« Reply #12 on: April 17, 2013, 10:30:35 AM »
Thanks to ALL OF YOU. Problem Solved!!

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Is Internet Connection Available?
« Reply #13 on: April 17, 2013, 10:37:34 AM »
Thanks to ALL OF YOU. Problem Solved!!

 8-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Is Internet Connection Available?
« Reply #14 on: April 17, 2013, 11:00:34 AM »
Good solution Ron, not reliant on Express Tools :-)
As is evident from my post, I wasn't aware of the 'ping' application :oops: