-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpingClient.rb
55 lines (46 loc) · 1.23 KB
/
pingClient.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# creates a UDP ping client
# can be paired with Java/System/UDPPingServer.java for testing
require 'socket'
# server information: IP address, port number, & timeout
SERVER = "127.0.0.1"
PORT = 1234
TIMEOUT = 1
class UDPPingClient
def initialize(seq)
@seq = seq
@time_start = Time.now
end
# send out and process all the pings
def request
# send initial UDP ping packet
socket = nil
str = "PING "+@seq+" "+@time_start.to_f.to_s+"\r\n"
begin
socket = UDPSocket.open
socket.send(str,0,SERVER,PORT)
# receive packet (if not dropped) and calculate the RTT
@test = 0
while (Time.now.to_f - @time_start.to_f) < 1
if select([socket],nil,nil,TIMEOUT)
socket.recv(4096)
@time_elapsed = (Time.now.to_f - @time_start.to_f)*1000
@rtt = @time_elapsed.to_s
@test = 1
end
end
# output to client with RTT in ms
puts "PING "+@seq+" Timeout - Network Error\r\n\r\n" if @test == 0
puts "PING "+@seq+" "+@rtt+" ms\r\n\r\n" if @test == 1
# cleanup
rescue IOError, SystemCallError
ensure
socket.close if socket
end
end
end
# execution of program, ping a total of 10 times
for ct in 0..9 do
count = ct.to_s
UDPPingClient.new(count).request
end
puts "PING process complete"