create a tcp server in ruby

To create a TCP server in Ruby, you need to use the socket module. Here is a basic example:

main.rb
require 'socket'

server = TCPServer.new('localhost', 3000)

loop do
  client = server.accept
  client.puts "Hello! You've connected to the server."
  client.close
end
168 chars
10 lines

This code sets up a TCP server that listens on port 3000 of the localhost. It enters an infinite loop that accepts incoming connections and sends a greeting message to each client before closing the connection.

Of course, this is just a starting point - depending on your needs, you may need to add more functionality to your server.

gistlibby LogSnag