Network connection test


(Pierre Brengard) #1

Hello,

Has someone any clue to write a test to see if the network is accessible
rather than this ugly method ? :

require 'socket’
def test_network_connection
begin
addr = IPSocket.getaddress
TCPSocket.gethostbyname(‘localhost’)[0]
baddr = addr.sub (/.\d+$/, “.255”)
puts "Broadcasting on #{baddr}"
case baddr
when /127./
return false
end

sock = UDPSocket.open
sock.send("HELLO", 0, baddr, 27666)
sock.close
return true
   rescue Exception
     sock.close
     return false
   end

end

Pierre Brengard


(Yohanes Santoso) #2

Pierre Brengard pbrengard@bct-technology.com writes:

Hello,

Has someone any clue to write a test to see if the network is
accessible rather than this ugly method ? :

require 'socket’
def test_network_connection
begin
addr = IPSocket.getaddress
TCPSocket.gethostbyname(‘localhost’)[0]

Um… localhost (127.0.0.1) is always accessible in UNIX. I believe
the same holds for Windows too. Also, 127.0.0.0/255 is always
accessible too. It is a predefined connection in any TCP stack I’ve
encountered (VMS, UNIX, Windows). No packet is ever sent on the wire
when connecting to 127.0.0.1.

I believe that testing connection to your gateway produce at least a
good method to ensure that at least connection to your gateway is
still functioning. Unfortunately, the method of getting the gateway of
a particular machine is platform dependent.

YS.