1 14 15 package chatserver; 16 17 import java.net.*; 18 import java.io.*; 19 import java.util.*; 20 import org.quickserver.net.AppException; 21 22 26 class TestClientThread extends Thread { 27 private Socket socket; 28 private BufferedReader in; 29 private PrintWriter out; 30 public static int counter = 0; 31 private int id = counter++; 32 private static int threadcount = 0; 33 private Random random = new Random(); 34 35 public static int threadCount() { 36 return threadcount; 37 } 38 39 40 public TestClientThread(InetAddress addr,int port) { 41 threadcount++; 42 try { 43 socket = new Socket(addr,port); 44 } catch(IOException e) { 45 System.err.println("Socket failed : "+e); 46 return; 47 } 48 49 try { 50 in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 51 out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), false); 53 start(); 54 } catch(IOException e) { 55 System.out.println("Socket Error : "+e); 56 try { 57 socket.close(); 58 } catch(IOException e2) { 59 System.err.println("Socket not closed"); 60 } 61 } 62 } 63 64 public void run() { 65 try { 67 Thread.sleep(200); 68 69 String str; 70 str = in.readLine(); 72 if(str==null) throw new IOException("Lost connection!"); 73 in.readLine(); 74 in.readLine(); 75 76 Thread.sleep(500); 77 78 in.readLine(); 80 out.println("User-"+id);out.flush(); 81 in.readLine(); 82 out.println("User-"+id);out.flush(); 83 in.readLine(); 84 out.println("home");out.flush(); 85 in.readLine(); 87 88 for(int i = 0; i < 7; i++) { 90 in.readLine(); 91 } 92 93 do { 94 str = in.readLine(); 95 if(str==null) 96 throw new IOException("Lost connection!"); 97 if(str.startsWith("{system.help}")==false && 98 str.startsWith("{user.info:")==false) { 99 System.out.println("["+id+"] Got: "+str); 100 } 101 if(str.endsWith("quit")){ 102 socket.close(); 103 socket = null; 104 } else if(str.indexOf("sendBack ")!=-1){ 105 out.print(str.substring(str.indexOf("sendBack ")+9)); 106 out.print("\r\n"); 107 out.flush(); 108 } 109 } while(socket!=null); 110 111 } catch(IOException e) { 112 System.err.println("["+id+"] IO Exception : "+e); 113 } catch(Exception e) { 114 System.err.println("["+id+"] Exception : "+e); 115 } finally { 116 try { 118 if(socket!=null) socket.close(); 119 } catch(IOException e) { 120 System.err.println("Socket not closed"); 121 } 122 threadcount--; } 124 } 125 } 126 127 public class TestChatServer { 128 public static int MAX_THREADS = 100; 129 private static Random random = new Random(); 130 131 public static void main(String [] args) 133 throws IOException, InterruptedException { 134 int port=7412; 135 InetAddress addr = InetAddress.getByName(null); 136 137 if(args.length > 0) { 138 try { 139 port = Integer.parseInt(args[0]); 140 } catch(NumberFormatException nfe) {} 141 } 142 143 if(args.length>=2) { 144 addr = InetAddress.getByName(args[1]); 145 } 146 147 if(args.length>=3) { 148 try { 149 MAX_THREADS = Integer.parseInt(args[2]); 150 } catch(NumberFormatException nfe) {} 151 } 152 153 if(args.length>=4) { 154 try { 155 TestClientThread.counter = Integer.parseInt(args[3]); 156 } catch(NumberFormatException nfe) {} 157 } 158 159 do { 160 if(TestClientThread.threadCount() < MAX_THREADS) 161 new TestClientThread(addr,port); 162 else 163 break; 164 Thread.currentThread().sleep(getSleepTime()); 165 } while(true); 166 System.out.println("All "+MAX_THREADS+" clients are ready!"); 167 } 168 169 private static int getSleepTime() { 170 return 200; 171 } 172 } 173 | Popular Tags |