Checking for Network Connection

OK, so, I quickly googled and was not able to find anything specific, but
what I'm trying to do is find an elegant way to check for a network/internet
connection. (I specifically need the internet, but a network connection
needs to be preset as well for that... except for dial up? Gah.)

So, from your collection experiences, what do you think is the best, most
elegant way to check for a network connection or an internet connection?
Should I just ping an address?

And, sorry if I missed something obvious.

M.T.

This isn't easy to do, in the general case. I assume you're just
trying to find out if you have a live connection to the WWW (including
firewall openings and DNS availability), so you could try this:

TCPSocket.new( "www.rubyforge.org", 80)

If it doesn't throw anything, you're golden. Make sure to add a
timeout loop (I usually wait no more than 4 seconds) because depending
on what is wrong, this call may hang for a long time. Don't use ping-
many firewalls block it.

···

On 8/30/06, Matt Todd <chiology@gmail.com> wrote:

OK, so, I quickly googled and was not able to find anything specific, but
what I'm trying to do is find an elegant way to check for a network/internet
connection. (I specifically need the internet, but a network connection
needs to be preset as well for that... except for dial up? Gah.)

So, from your collection experiences, what do you think is the best, most
elegant way to check for a network connection or an internet connection?
Should I just ping an address?

And, sorry if I missed something obvious.

M.T.

Thanks... that was also a solution I had thought about, but couldn't
really manifest with my limited experience.

I'm not exactly sure how to do a timeout loop... or, rather, what you
mean. Do you want me to try three times? Or perhaps to say "timeout
after this amount of time"? Is that part of TCPSocket#new ?

:smiley:

M.T.

I'm sure there is a timeout option for the socket call, or you could
just do something like this:

require 'timeout'
timeout(10) do
  # connect
end

···

On 8/30/06, Matt Todd <chiology@gmail.com> wrote:

Thanks... that was also a solution I had thought about, but couldn't
really manifest with my limited experience.

I'm not exactly sure how to do a timeout loop... or, rather, what you
mean. Do you want me to try three times? Or perhaps to say "timeout
after this amount of time"? Is that part of TCPSocket#new ?

:smiley:

M.T.

I'm sure there is a timeout option for the socket call, or you could
just do something like this:

require 'timeout'
timeout(10) do
  # connect
end

Sweet! I didn't know that library existed! You learn something new every day.

Thanks guys.

M.T.