server.rb 482 B

12345678910111213141516171819202122
  1. # frozen_string_literal: true
  2. # sharable_constant_value: literal
  3. require 'socket'
  4. port = 2000
  5. puts "Starting server at port #{port}"
  6. server = TCPServer.new port
  7. loop do
  8. Thread.start(server.accept) do |socket|
  9. puts "New connection from #{socket.peeraddr[3]} port #{socket.peeraddr[1]}."
  10. while (text = socket.gets)
  11. puts "Received data: #{text.dump}"
  12. end
  13. puts 'No more data is received.'
  14. rescue StandardError => e
  15. warn "Error: #{e.message}"
  16. end
  17. end