How to Write a TCP Echo Server in Ruby

Mon, Aug 24, 2015

Read in 1 minutes

In ruby you have lowlevel access to all the same methods that you do in C. But you also have some higher level sugar on top of that - and a program that does the same as the c version, is almost as concise as it is in javascript. You get no free concurrency here though - you still need multiple processes or threads to achieve that.

require 'socket'
a = TCPServer.new('', ARGV.first)
connection = a.accept
loop do
  connection.write connection.recv(1024) 
end

See Also

How to Write Json in Ruby

Fri, Sep 11, 2015

Read more →

How to Parse Json in Ruby

Thu, Sep 10, 2015

Read more →

How to Read from stdin in Ruby

Fri, Aug 28, 2015

Read more →

How to write a TCP Echo server in Javascript

Fri, Aug 21, 2015

Read more →

How to Write a TCP Echo Server in C

Fri, Aug 21, 2015

Read more →