Class: Net::PingUDP

Inherits:
Ping
  • Object
show all
Defined in:
lib/net/ping.rb

Overview

With a UDP ping, send a simple text string and check the return string. If they match, assume success.

Constant Summary

Constants inherited from Ping

Net::Ping::VERSION

Instance Attribute Summary collapse

Attributes inherited from Ping

#exception, #host, #port, #timeout, #warning

Instance Method Summary collapse

Methods inherited from Ping

#initialize

Constructor Details

This class inherits a constructor from Net::Ping

Instance Attribute Details

#dataObject

Returns the value of attribute data.



96
97
98
# File 'lib/net/ping.rb', line 96

def data
  @data
end

Instance Method Details

#pingObject Also known as: ping?

Sends a simple text string and checks the return string. If they match, the ping was successful.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/net/ping.rb', line 100

def ping
   super
   success = false
   udp = UDPSocket.open
   a = []
   begin
      Timeout.timeout(@timeout){
         udp.connect(@host, @port)
         udp.send(@data, 0)
         a = udp.recvfrom(64)
      }
   rescue Exception => err
      @exception = err
   else
      if a[0] == @data
         success = true
      end
   ensure
      udp.close if udp
   end
   success
end