Skip to content

Instantly share code, notes, and snippets.

Created December 2, 2015 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7fc1a347c2ab14151b5b to your computer and use it in GitHub Desktop.
Save anonymous/7fc1a347c2ab14151b5b to your computer and use it in GitHub Desktop.
require 'socket' # Get sockets from stdlib
port=666
#dbm = YAML::DBM.new("fruitstore.db")
#build hash
#ask if key exist
#ask if value exist
hash=Hash.new
server = TCPServer.open(port) # Socket to listen on port 2000
loop { # Servers run forever
client = server.accept # Wait for a client to connect
until client.closed?
question=client.recv(500)
if question.include?("build_hash")
question.gsub!("build_hash","")
client.puts "building hash"
key=question.split(" ")[0]
v_1=question.split(" ")[1]
v_2=question.split(" ")[2]
hash[key]=v_1,v_2
end
if question.include?("got_key")
puts question.gsub!("got_key","")
response=hash[question]
if response.nil?
client.puts("nil")
else
client.puts(response)
end
#client.puts "getting key"
end
if question.include?("got_value")
puts question.gsub!("got_value","")
response=hash.key[question]
if response.nil?
client.puts("nil")
else
client.puts(response)
end
end
puts "the hash is now: #{hash.size}"
end#until closed
}
#the client ran in another console
s = TCPSocket.open(hostname, 666)
s.puts("build_hashNew hash entry")
#hash => key=>[val,ue]
puts s.recv(500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment