/**
 * BoringServer.java
 * A simple multithreaded server which servres 2000 random ints.
 */
 
import java.io.*;
import java.net.*;

public class BoringServer {
	static ServerSocket srvSock = null;
	public static void main(String [] args) {
		int port = Integer.parseInt(args[0]);
		try {
			srvSock = new ServerSocket(port);
			while(true) {
				Socket connection = srvSock.accept();
				(new Worker(connection)).start();
			}
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	// Override finalize() to close server socket
    protected void finalize() {
        if (srvSock != null) {
            try {
                srvSock.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            srvSock = null;
        }
    }
}

class Worker extends Thread {
	Socket sock = null;
	Worker(Socket s) {
		sock = s;
	}
	
	public void run() {
		try {
			PrintWriter pw = new PrintWriter(sock.getOutputStream());
			for(int i = 0; i < 2000; i++) {
				pw.println(randInt(1000));
				pw.flush();
			}	
			pw.close();
			sock.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
							
	public int randInt(int max) {
		return  (int) (max * Math.random());
	}
}