Skip to content

Instantly share code, notes, and snippets.

@marceloboeira
Last active September 10, 2016 15:34
Show Gist options
  • Save marceloboeira/0deaaf800a126aac42a5bb3c868838e7 to your computer and use it in GitHub Desktop.
Save marceloboeira/0deaaf800a126aac42a5bb3c868838e7 to your computer and use it in GitHub Desktop.
require "socket"
class Server
def self.start
server = TCPServer.new("localhost", 5000)
server.recv_buffer_size = 4096
data = Hash(String, String).new
loop do
socket = server.accept
if socket
spawn do
loop do
if request = socket.gets
request = request.split(" ").map{|item| item.strip }
command = request[0]
key = request[1]
if command == "set"
value = request[2]
data[key] = value
socket.puts(value)
elsif command == "get"
value = data[key]
socket.puts(value)
else
socket.puts("error: #{command} is not a valid command")
end
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment