Created
April 20, 2012 15:34
-
-
Save zxiest/2429661 to your computer and use it in GitHub Desktop.
Mini Webserver Example
This file contains hidden or 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
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