1 package org.sapia.ubik.net; 2 3 4 12 public class MyServer extends SocketServer { 13 16 public MyServer() throws java.io.IOException { 17 super(6666, new MyThreadPool(), new DefaultUbikServerSocketFactory()); 18 } 19 20 public static void main(String [] args) { 21 try { 22 MyServer svr = new MyServer(); 23 Thread t = new Thread (svr); 24 t.start(); 25 svr.waitStarted(); 26 System.out.println("Server started"); 27 28 while (true) { 29 Thread.sleep(100000); 30 } 31 } catch (Exception e) { 32 e.printStackTrace(); 33 } 34 } 35 36 static class MyThreadPool extends ThreadPool { 37 MyThreadPool() { 38 super("myThread", true, 10); 39 } 40 41 44 protected PooledThread newThread() throws Exception { 45 return new MyPooledThread(); 46 } 47 } 48 49 static class MyPooledThread extends PooledThread { 50 53 protected void doExec(Object task) { 54 Connection conn = ((Request) task).getConnection(); 55 56 try { 57 while (true) { 58 System.out.println(conn.receive()); 59 conn.send("Hello"); 60 } 61 } catch (Throwable t) { 62 conn.close(); 63 t.printStackTrace(); 64 } 65 } 66 } 67 } 68 | Popular Tags |