Skip to content

Instantly share code, notes, and snippets.

@nwestfall
Created May 18, 2017 14:28
Show Gist options
  • Save nwestfall/159074b70b9f14a1f7bbacad73f2c01f to your computer and use it in GitHub Desktop.
Save nwestfall/159074b70b9f14a1f7bbacad73f2c01f to your computer and use it in GitHub Desktop.
Ruby Socket Client Console Tester
require 'socket'
class App
def self.send(ip, port, file, delay)
puts "Starting up Socket Client on #{ip}:#{port} with a delay of #{delay} seconds"
file_content = File.read(file)
puts "File read"
while true
begin
socket = TCPSocket.open(ip, port)
#puts "Socket Client created"
#puts "Sending data..."
socket.puts file_content + "\\EOF"
#puts "Data sent..."
socket.close
sleep(delay.to_f)
rescue
ensure
puts "Error Sending"
end
end
end
def self.sendtwo(ip, port, file, delay)
puts "Starting up Socket Client on #{ip}:#{port} with a delay of #{delay} seconds"
file_content = File.read(file)
puts "File read"
socket = TCPSocket.open(ip, port)
puts "Socket Client created"
while true
begin
#puts "Sending data..."
socket.puts file_content + "\\EOF"
#puts "Data sent..."
sleep(delay.to_f)
rescue
puts "Error Sending"
ensure
end
end
socket.close
end
end
puts "Welcome to the Ruby Socket Client!\n"
puts "Available commands...\n"
puts "To Start Socket Client - start -ip -port -file -delay\n"
puts "To Start (v2) Socket Client - start2 -ip -port -file -delay\n"
puts "To Close Socket Client - close\n"
puts "\n"
loop do
input = gets.chomp
command, *params = input.split /\s/
case command
when 'start'
if params.length == 4
App.send(params[0], params[1], params[2], params[3])
else
puts "Invalid Parameters"
end
when 'start2'
if params.length == 4
App.sendtwo(params[0], params[1], params[2], params[3])
else
puts "Invalid Parameters"
end
when 'close'
break
else
puts 'Invalid Command'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment