Skip to content

Instantly share code, notes, and snippets.

@sai43
Created June 6, 2015 17:47
Show Gist options
  • Save sai43/74846b77d8ab85ee4358 to your computer and use it in GitHub Desktop.
Save sai43/74846b77d8ab85ee4358 to your computer and use it in GitHub Desktop.
Simple Ruby Client/Server Communication
require 'socket'
server = TCPSocket.open("127.0.0.1", 9090)
puts "You have the following options on this devilish server: \r"
puts "Type '%time' to see the time \r"
puts "Type '%hastalavista' to see the exit \r"
puts "Type '%pie' for some pie \r"
puts "Type '%why' to get some answer \r"
puts "Type '%Showtime' to get the image \r"
puts "Type '%nyan' to listen some music \r"
while true
request = gets.chomp
case request
when "%Showtime"
server.write(request)
f = File.new("Showtime.jpg", "wb")
command = server.recv(20260)
f.write(command)
f.close
puts "Take a look into the client directory\r"
when "%hastalavista"
server.write(request)
command = server.recv(1024)
puts "#{command}\r"
when "%close"
puts "The Connection is lost\r"
server.close
else
server.write(request)
command = server.recv(1024)
puts "#{command}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment