Type:
Class
TCPServer represents a TCP/IP server socket.
A simple TCP server may look like:
1 2 3 4 5 6 7 8 9 | require 'socket' server = TCPServer. new 2000 # Server bind to port 2000 loop do client = server.accept # Wait for a client to connect client.puts "Hello !" client.puts "Time is #{Time.now}" client.close end |
A more usable server (serving multiple clients):
1 2 3 4 5 6 7 8 9 10 | require 'socket' server = TCPServer. new 2000 loop do Thread .start(server.accept) do |client| client.puts "Hello !" client.puts "Time is #{Time.now}" client.close end end |