Created
June 6, 2015 17:47
-
-
Save sai43/74846b77d8ab85ee4358 to your computer and use it in GitHub Desktop.
Simple Ruby Client/Server Communication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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