Skip to content

Instantly share code, notes, and snippets.

@zxiest
Created April 20, 2012 15:34
Show Gist options
  • Save zxiest/2429661 to your computer and use it in GitHub Desktop.
Save zxiest/2429661 to your computer and use it in GitHub Desktop.
Mini Webserver Example
require 'socket' # Get sockets from stdlib
filename = "file.txt"
port = 80
server = TCPServer.open(port) #open socket for listening
loop { # Servers run forever
client = server.accept # Wait for a client to connect
file = File.new(filename, "r")
while (line = file.gets)
client.puts "#{line}"
end
file.close
client.close # Disconnect from the client
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment