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
accept_nonblock
  • References/Ruby on Rails/Ruby/Classes/TCPServer

tcpserver.accept_nonblock => tcpsocket Instance Public methods Accepts an

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Ruby/Classes/TCPServer

TCPServer.new([hostname,] port) => tcpserver Class Public methods

2025-01-10 15:47:30
accept
  • References/Ruby on Rails/Ruby/Classes/TCPServer

tcpserver.accept => tcpsocket Instance Public methods

2025-01-10 15:47:30
listen
  • References/Ruby on Rails/Ruby/Classes/TCPServer

socket.listen( int ) => 0 Instance Public methods Listens for connections

2025-01-10 15:47:30
sysaccept
  • References/Ruby on Rails/Ruby/Classes/TCPServer

tcpserver.sysaccept => file_descriptor Instance Public methods Returns a

2025-01-10 15:47:30