client.rb 512 B

1234567891011121314151617181920212223242526
  1. # frozen_string_literal: true
  2. # sharable_constant_value: literal
  3. require 'socket'
  4. remote = '127.0.0.1'
  5. port = 2000
  6. timeout = 60
  7. begin
  8. puts "Connecting to #{remote} port #{port} with timeout #{timeout}."
  9. client = Socket.tcp(remote, port, connect_timeout: timeout)
  10. puts 'Connected.'
  11. 20.times do |index|
  12. puts "Sending message #{index + 1}."
  13. client.puts "Message #{index + 1}: Hello World!"
  14. end
  15. puts 'Close socket.'
  16. client.close
  17. rescue StandardError => e
  18. warn "Error: #{e}"
  19. exit 1
  20. end