From: Martin Ankerl Date: 2005-12-02T02:57:30+09:00 Subject: Re: Good Ruby Examples? > So, what's everybody's favorite smallish examples of the power of > coding with Ruby. This is my favourite, here are two solutions for the task "Write a threaded server that offers the time" (Ruby example is taken from the book 'The Ruby Way'): # Ruby 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 // And the functional equivalent in Java: 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(); } } } -- martinus | http://martinus.geekisp.com/