create a tcp server on a specific port in ruby

To create a TCP server in Ruby, you first need to require the socket library. Then, you can create a new TCP server using the TCPServer.new() method, specifying the port number on which you want to listen for incoming connections.

Here's an example of how to create a TCP server on port 3000:

main.rb
require 'socket'

server = TCPServer.new(3000)

loop do
  client = server.accept

  # do something with the client connection
end
130 chars
10 lines

In this example, the server variable holds the new TCP server listening on port 3000. The loop statement will continuously accept incoming connections and assign them to a new client variable.

You can then use the client connection to send and receive data from the connected client.

gistlibby LogSnag