Page 1 of 1

Fast way to check for internet connection?

Posted: Mon Oct 19, 2009 2:00 pm
by andyh1234
Is there a quick way to test for an internet connection?

I have a few lines of code that need to grab data from the internet, for example the check for update.

I simply use the 'put url "http://xyz" into tResult' command, but if the computer is not connected to the net it freezes the app for about 4 seconds, then carries on.

I think ive got round it in a few places by using a 'send in 1' to split the process off, but there are a couple of places I need to either get the result from the net, or tell the user they need a connection and the 4 second lock out looks pretty poor.

Any ideas?

Thanks

Andy

Posted: Mon Oct 19, 2009 2:40 pm
by Philhold
Sarah Reichelt

Code: Select all

on mouseUp
  put "" into fld 1 of cd 1
  open socket "www.yahoo.com" with message "openSock"
  send checkLink to me in 6 seconds
  send mouseUp to me in 300 seconds
end mouseUp

on openSock
  put "Link OK" into fld 1 of cd 1
  close socket "www.yahoo.com"
end openSock

on socketError
  put "Link down" into fld 1 of cd 1
end socketError

on checkLink
  if fld 1 of cd 1 is empty then
    put "Link down" into fld 1 of cd 1
  end if
end checkLink

Jerry Daniels:

Code: Select all

function haveIPconnect theHostName
  if colon is not in theHostName then
    put colon & "80|test" after theHostName
  end if
  open socket theHostName
  if theHostName is among the lines of the openSockets then
    close socket theHostName
    return true
  else
    beep
    answer warning "Either the upload site is down or" & return & "you are
not connected to the internet!"
    return false
  end if
end haveIPconnect

Posted: Mon Oct 19, 2009 2:47 pm
by andyh1234
Thanks.

Im afraid just tried both, and they are having the same effect so I guess it might be my mac.

Without a connection they both lock my mac up for about 4 seconds as soon as I call them.

Ill switch over to windows and see if that has the same effect, and try putting them in another stack see if that helps.

Posted: Mon Oct 19, 2009 5:25 pm
by Mark
Hi andyh1234,

Does this work for you?

Code: Select all

function connectionAlive
  put "255.255.255.255:44444" into mySock
  open datagram socket to mySock
  put hostAddress(mySock) into myIP1
  close socket mySock
  if myIP1 is "127.0.0.1" then
    return false
  else
    return true
  end if
end connectionAlive
Best,

Mark

Posted: Mon Oct 19, 2009 6:33 pm
by andyh1234
Thanks Mark, that helps.

If my computer is not connected to its router, then it works (my computer gets an address of 127.0.0.1), however if I disconnect the router from the phone line, the computer still gets an internal ip from the router, eg 192.168.0.7.

Its much better for totally offline computers, so thanks again!! At least this way the only people with 'slow' performance on the internet checks will be those that have a problem with their internet connection, rather than those that are totally offline.

Andy