$ ruby server.rb
Created
July 19, 2018 04:41
-
-
Save luk3thomas/2494874ba2650cd44bd79cf8a00cf79a to your computer and use it in GitHub Desktop.
Echo server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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