Fast way to check for internet connection?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
andyh1234
Posts: 476
Joined: Mon Aug 13, 2007 4:44 pm
Contact:

Fast way to check for internet connection?

Post by andyh1234 » Mon Oct 19, 2009 2:00 pm

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

Philhold
Posts: 64
Joined: Thu Mar 05, 2009 3:39 pm

Post by Philhold » Mon Oct 19, 2009 2:40 pm

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

andyh1234
Posts: 476
Joined: Mon Aug 13, 2007 4:44 pm
Contact:

Post by andyh1234 » Mon Oct 19, 2009 2:47 pm

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.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Oct 19, 2009 5:25 pm

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
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

andyh1234
Posts: 476
Joined: Mon Aug 13, 2007 4:44 pm
Contact:

Post by andyh1234 » Mon Oct 19, 2009 6:33 pm

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

Post Reply