Multi-Client TCP Servers : TCP Server « Network « Ruby






Multi-Client TCP Servers

require 'socket'

server = TCPServer.new(1234)

loop do
  Thread.start(server.accept) do |connection|
    while line = connection.gets
      break if line =~ /quit/
      puts line
      connection.puts "Received!"
    end

    connection.puts "Closing the connection. Bye!"
    connection.close
  end
end

 








Related examples in the same category

1.Building a Simple TCP Server
2.To create a simple Web server, you can accept connections on the IP address 0.0.0.0 and the port number 80
3.Creating a Web Server with thread support