import javax.imageio.*; import java.awt.image.*; import java.awt.*; import java.security.NoSuchAlgorithmException; import java.security.MessageDigest; import java.lang.reflect.*; import java.net.*; import java.io.*; import javax.swing.*; import java.util.concurrent.*; import java.util.regex.*; import java.util.List; import java.util.zip.*; import java.util.*; public class main { static String helloString; static String programID; // filled by JavaX static int port; // use random port public static void main(String[] args) throws Exception { helloString = "Hello. This is #" + programID; // my hello string port = installHelloMessage(helloString); print("Hello world. Sleeping. Port=" + port); sleep(); } // returns port number static int installHelloMessage(final String helloString) { try { final ServerSocket serverSocket = new ServerSocket(0); int port = serverSocket.getLocalPort(); //serverSocket.setSoTimeout(1000); Thread _t = new Thread() { public void run() { try { // todo: higher priority while (true) { try { final Socket s = serverSocket.accept(); print("connect"); Thread _t = new Thread() { public void run() { try { try { Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); w.write(helloString + "\n"); w.close(); //s.close(); // rather wait for client to disconnect. } finally { print("disconnect"); } } catch (Exception _e) { throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } } }; _t.start(); } catch (SocketTimeoutException e) { } } } catch (Exception _e) { throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } } }; _t.setDaemon(true); _t.start(); return port; } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} static void sleep(long ms) { try { Thread.sleep(ms); } catch (Exception e) { throw new RuntimeException(e); } } static void sleep() { try { synchronized(main.class) { main.class.wait(); } } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} static void print() { System.out.println(); } static void print(Object o) { System.out.println(o); } static void print(long i) { System.out.println(i); } }