Created
August 28, 2019 22:43
-
-
Save danielpaul/b9b4bd3bad2524e1bbb09e4bd98a14b2 to your computer and use it in GitHub Desktop.
Simple Ruby TCP Server
This file contains hidden or 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
# ruby socket.rb | |
require 'socket' # Sockets are in standard library | |
class Server | |
def initialize(socket_port) | |
@server_socket = TCPServer.new(socket_port) | |
puts 'Started server.........' | |
end | |
def run | |
loop { | |
client_connection = @server_socket.accept | |
Thread.start(client_connection) do |connection| # open thread for each accepted connection | |
line = connection.gets | |
puts line | |
connection.puts "Success!\n" | |
connection.close | |
end | |
}.join | |
end | |
end | |
Server.new(1234).run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment