Skip to content

Instantly share code, notes, and snippets.

@activeliang
Forked from Hersha-Snips/socket.rb
Created May 10, 2020 15:22
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 activeliang/f7a50de0e171a5770c8eeff6f5cfaea5 to your computer and use it in GitHub Desktop.
Save activeliang/f7a50de0e171a5770c8eeff6f5cfaea5 to your computer and use it in GitHub Desktop.
Ruby: Simple TCP Server
require 'socket' # Sockets are in standard library
server = TCPServer.new(1234)
begin
while connection = server.accept
while line = connection.gets
break if line =~ /quit/
puts line
connection.puts "Received!\n"
end
connection.puts "Closing the connection. Bye!\n"
connection.close
#server.close
end
rescue Errno::ECONNRESET, Errno::EPIPE => e
puts e.message
retry
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment