Building a Simple TCP Server : TCP Server « Network « Ruby






Building a Simple TCP Server

require 'socket'

server = TCPServer.new(1234)

while connection = server.accept
  while line = connection.gets
    break if line =~ /quit/
    puts line
    connection.puts "Received!"
  end

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


# To test
# telnet 127.0.0.1 1234

 








Related examples in the same category

1.Multi-Client TCP Servers
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