Skip to content

Instantly share code, notes, and snippets.

@Moligaloo
Created July 10, 2012 16:41
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 Moligaloo/3084547 to your computer and use it in GitHub Desktop.
Save Moligaloo/3084547 to your computer and use it in GitHub Desktop.
Simple Ruby Server & Client
require 'socket' # Sockets are in standard library
hostname = 'www.3-kingdoms.com'
port = 2000
s = TCPSocket.open(hostname, port)
while line = s.gets # Read lines from the socket
puts line.chop # And print with platform line terminator
end
s.close # Close the socket when done
require 'socket' # Get sockets from stdlib
server = TCPServer.open(2000) # Socket to listen on port 2000
loop { # Servers run forever
client = server.accept # Wait for a client to connect
client.puts(Time.now.ctime) # Send the time to the client
client.puts "Closing the connection. Bye!"
client.close # Disconnect from the client
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment