Skip to content

Instantly share code, notes, and snippets.

@sai43
Created June 6, 2015 17:44
Show Gist options
  • Save sai43/048fdd42cf61d04e22c8 to your computer and use it in GitHub Desktop.
Save sai43/048fdd42cf61d04e22c8 to your computer and use it in GitHub Desktop.
Simple Ruby client/server communication
require 'socket'
require 'win32/sound' #gem install win32-sound
require 'bigdecimal'
require 'bigdecimal/math'
include BigMath
include Win32
server = TCPServer.new("127.0.0.1",9090)
request = ""
bytest = ""
why = Array.new(11)
why = ["Because 41", "Because God wanted so", "Ch Sai approved it", "Because Destiny disagreed",
"Because Love Failure..!", "Because it's RED", "Because she is not accepted", "Because Sai doesn't have ....!",
"Because She is beautiful"]
loop {
Thread.start(server.accept) do |client|
while true do
request = client.recv(512).chomp
puts "#{request} coming to #{client.peeraddr[2]}: #{client.peeraddr[0]} from #{client.peeraddr[3]}:#{client.peeraddr[1]}"
case request
when "%time"
client.write("#{Time.now}\r")
when "%hastalavista"
client.write("See ya later..!")
client.close
server.shutown([how=2])
server.close
return 0
when "%pie"
client.write("I heard you want some pie..... Here you are some pie: \r\n pi= #{PI(41)}\r")
when "%nyan"
client.write("Enjoy the music\r")
sound.play('nyan.wav')
when "%why"
client.write("#{why[rand(11).to_i]}\r")
when /\?$/
client.write("41 \r")
when "%Showtime"
File.open("Terminator2.jpg", "rb") do |inp1|
while line = inp1.gets
bytest += line
end
client.send(bytest,0)
inp1.close
end
else
client.write("Can you elaborate on that ?\r")
end
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment