Monthly Archives: March 2006

Java vs. Ruby

Here are two example solutions for the problem

Write a threaded server that offers the time.

The first example is written in Java, the second in Ruby (taken from the excellent book “The Ruby Way”). Both versions are implemented to be very simple. Please judge for yourself:

Java (31 lines of code)

package at.martinus;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class TimeServer {
   private static class TellTime extends Thread {
     private Socket soc;

     public TellTime(Socket soc) {
       super();
       this.soc = soc;
     }

     public void run() {
       try {
         this.soc.getOutputStream().write(new Date().toString().getBytes());
       } catch (Exception e) {
       } finally {
         try {
           this.soc.close();
         } catch (IOException e1) {
         }
       }
     }
   }

   public static void main(String args[]) throws Exception {
     ServerSocket server = new ServerSocket(12345);
     while (true) {
       new TellTime(server.accept()).start();
     }
   }

}

Starting it:

javac at/martinus/TimeServer.java
java at.martinus.TimeServer -cp .

Ruby (8 lines of codes)

require "socket"

server = TCPServer.new(12345)

while (session = server.accept)
  Thread.new(session) do |my_session|
    my_session.puts Time.new
    my_session.close
  end
end

Starting it:

ruby timeserver.rb

Taken from my comment at comp.lang.ruby.