package test; import java.io.*; import java.net.*; import java.util.Date; import java.text.DateFormat; import java.util.Random; public class client { Socket s; int pauseMillisMin, pauseMillisMax; int msgLen; String msg; Random rand = new Random(); public client(InetAddress host, int sport, int pauseMillisMin, int pauseMillisMax, int msgLen) throws IOException { s = new Socket(host, sport); this.pauseMillisMin = pauseMillisMin; this.pauseMillisMax = pauseMillisMax; this.msgLen = msgLen; StringBuffer sb = new StringBuffer(msgLen+1); for (int i = 0; i < msgLen; i++) sb.append((char) (33 + Math.random()*93)); sb.append("\n"); msg = sb.toString(); (new Thread(new MyWriter(s.getOutputStream()))).start(); (new Thread(new MyReader(s.getInputStream()))).start(); } class MyWriter implements Runnable { OutputStream so; int pauseMillisSpread = 1 + pauseMillisMax - pauseMillisMin; MyWriter(OutputStream so) {this.so = so;} public void run() { OutputStreamWriter osr = new OutputStreamWriter(so); try { while (true) { osr.write(msg); int pauseMillis = pauseMillisMin; if (pauseMillisSpread > 1) pauseMillis += rand.nextInt(pauseMillisSpread); try { Thread.sleep(pauseMillis); } catch (Exception ex) {} } } catch (IOException ex) { int locport = s.getLocalPort(); System.err.println("At " + nowString() + ", aborting writing from " + locport + " because of exn " + ex.getClass().getName() + " msg=" + ex.getMessage()); } } } class MyReader implements Runnable { InputStream si; MyReader(InputStream si) {this.si = si;} public void run() { InputStreamReader isr = new InputStreamReader(si); BufferedReader br = new BufferedReader(isr); try { while (true) { String mr = br.readLine(); if (!msg.regionMatches(0, mr, 0, msgLen)) { System.err.println("Message mismatch! Sent [" + msg + "] received [" + mr + "]."); return; } } } catch (IOException ex) { int locport = s.getLocalPort(); System.err.println("At " + nowString() + ", aborting reading through " + locport + " because of exn " + ex.getClass().getName() + " msg=" + ex.getMessage()); } } } public static void main(String[] args) throws IOException { if (args.length != 6) { System.err.println("Usage: client host port nThreads pauseMillisMin pauseMillisMax msgLen"); System.exit(1); } String hostStr = args[0]; int sport = Integer.parseInt(args[1]); int nThreads = Integer.parseInt(args[2]); int pauseMillisMin = Integer.parseInt(args[3]); int pauseMillisMax = Integer.parseInt(args[4]); int msgLen = Integer.parseInt(args[5]); InetAddress host = InetAddress.getByName(hostStr); System.out.println("Testing host " + host + ", port " + sport + ", n-thds=" + nThreads + ", pause-ms=[" + pauseMillisMin + ", " + pauseMillisMax + "]" + ", msglen=" + msgLen); for (int i = 0; i < nThreads; i++) { new client(host, sport, pauseMillisMin, pauseMillisMax, msgLen); } System.out.println("Started at " + nowString()); } private static final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); private static String nowString() { Date dNow = new Date(); return df.format(dNow); } }