Skip to content

Instantly share code, notes, and snippets.

@bracki
Created November 29, 2012 17:32
Show Gist options
  • Save bracki/4170624 to your computer and use it in GitHub Desktop.
Save bracki/4170624 to your computer and use it in GitHub Desktop.
Simple client
require 'socket'
@functions = {
:ADD => lambda {|a, b| a + b},
:SUBTRACT => lambda {|a, b| a - b},
:MULTIPLY => lambda {|a, b| a * b}
}
def build_response(msg)
m = msg.split(':')
func = m[0]
uid = m[1]
args = m[2..-1].map {|e| e.to_i}
result = args.reduce(&@functions[func.to_sym])
return "#{uid}:#{result}"
end
def main
s = UDPSocket.new
s.bind('localhost', 9000)
while true do
msg = s.recv(4096)
break if msg == '__SHUTDOWN__'
s.send(build_response(msg), 0, 'localhost', 9001)
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment