FileDocCategorySizeDatePackage
CalcClient.javaAPI DocExample1211Sun Dec 07 10:45:00 GMT 2003javathreads.examples.ch12.example4

CalcClient

public class CalcClient extends Thread

Fields Summary
long
n
Socket
sock
Constructors Summary
public CalcClient(long n, String host, int port)

        this.n = n;
	sock = new Socket(host, port);
    
Methods Summary
public static voidmain(java.lang.String[] args)

        int nThreads = Integer.parseInt(args[0]);
        long n = Long.parseLong(args[1]);
        int port = Integer.parseInt(args[3]);
        CalcClient[] clients = new CalcClient[nThreads];
        for (int i = 0; i < nThreads; i++) {
            clients[i] = new CalcClient(n, args[2], port);
            clients[i].start();
        }
        for (int i = 0; i < nThreads; i++) {
            clients[i].join();
        }
    
public voidrun()

        try {
            DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
            dos.writeLong(n);
            DataInputStream dis = new DataInputStream(sock.getInputStream());
            System.out.println("Received answer " + dis.readLong());
        } catch (IOException ioe) {
            System.out.println("Socket error " + ioe);
        }