Skip to content

Instantly share code, notes, and snippets.

@luk3thomas
Created July 19, 2018 04:41
Show Gist options
  • Save luk3thomas/2494874ba2650cd44bd79cf8a00cf79a to your computer and use it in GitHub Desktop.
Save luk3thomas/2494874ba2650cd44bd79cf8a00cf79a to your computer and use it in GitHub Desktop.
Echo server
$ ruby server.rb
#!/usr/bin/env ruby
require 'socket'
host = ARGV[2] || '127.0.0.1'
port = (ARGV[1] || 8080).to_i
server = TCPServer.new(host, port)
$stderr.puts "Server listening on #{host}:#{port}\n\n"
begin
loop do
session = server.accept
puts Time.now
puts session.readpartial 1_000_000_000
puts
session.puts "HTTP/1.1 200 OK"
session.puts "\r\n"
session.close()
end
rescue SystemExit, Interrupt
server.close()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment