import javax.imageio.*; import java.awt.image.*; import java.awt.event.*; import java.awt.*; import java.security.NoSuchAlgorithmException; import java.security.MessageDigest; import java.lang.reflect.*; import java.net.*; import java.io.*; import javax.swing.text.*; import javax.swing.event.*; import javax.swing.*; import java.util.concurrent.atomic.*; import java.util.concurrent.*; import java.util.regex.*; import java.util.List; import java.util.zip.*; import java.util.*; public class main { public static void main(String[] args) throws Exception { installHelloMessage("Hello"); sleep(); } // returns port number static int installHelloMessage(final String helloString) { try { final ServerSocket serverSocket = makeServerSocket(); int port = serverSocket.getLocalPort(); print("hello message on port: " + port); //serverSocket.setSoTimeout(1000); Thread _t_0 = new Thread() { public void run() { try { // todo: higher priority while (true) { try { final Socket s = serverSocket.accept(); print("connect"); Thread _t_1 = new Thread() { public void run() { try { try { Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); w.write(helloString + "\n"); w.flush(); final BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream(), "UTF-8")); while (true) { String line = in.readLine(); if (line == null) break; print("> incoming: " + line); if ("kill".equals(line) || "kill!".equals(line)) if (s.getInetAddress().isLoopbackAddress()) killMyself(); else print("Ignoring kill command from non-local: " + s.getInetAddress()); } } finally { print("disconnect"); } } catch (Exception _e) { throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } } }; _t_1.start(); } catch (SocketTimeoutException e) { } } } catch (Exception _e) { throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } } }; _t_0.setDaemon(true); _t_0.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 ServerSocket makeServerSocket() { try { for (int port = 10000; port < 65535; port++) { try { return new ServerSocket(port); } catch (IOException e) { // try next port } } throw fail("No free port found (tried 10000-65535)"); } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} static void killMyself() { print("Killing myself. (insert overall feeling here)"); System.exit(0); } static void print() { System.out.println(); } static void print(Object o) { System.out.println(o); } static void print(long i) { System.out.println(i); } static RuntimeException fail() { throw new RuntimeException("fail"); } static RuntimeException fail(String msg) { throw new RuntimeException(msg); } }