package ns.Client;

public class Client {
    public static void main(String[] args) {
        if (args.length < 1) {
            System.err.println("No domain name given. Usage: ./client [domain name]");
            return;
        }
        String domain = args[0];
        System.out.println("Looking up " + domain + "…");
    }
}
@@ -17,29 +17,2204 @@
 t;%0A%0A
-public class Client %7B
+import java.io.BufferedReader;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.io.IOException;%0Aimport java.io.OutputStream;%0Aimport java.net.Socket;%0Aimport java.net.UnknownHostException;%0A%0Apublic class Client %7B%0A    /**%0A     * Regex for checking if a thing looks kinda like an IP address.%0A     */%0A    private static final String IP_REGEX =%0A        %22%5C%5Cd%7B1,3%7D%5C%5C.%5C%5Cd%7B1,3%7D%5C%5C.%5C%5Cd%7B1,3%7D%5C%5C.%5C%5Cd%7B1,3%7D%22;%0A%0A    /**%0A     * Naming service server to connect to.%0A     */%0A    private String host;%0A%0A    /**%0A     * Port number to connect to.%0A     */%0A    private int port;%0A%0A    /**%0A     * Connection to the name server.%0A     */%0A    private Socket sock;%0A%0A    /**%0A     * Create a NS client instance.%0A     *%0A     * @param host Hostname to connect to.%0A     * @param port Port number to connect to.%0A     */%0A    public Client(String host, int port) %7B%0A        this.host = host;%0A        this.port = port;%0A    %7D%0A%0A    /**%0A     * Connect to the name server.%0A     */%0A    protected void connect() throws IOException, UnknownHostException %7B%0A        this.sock = new Socket(this.host, this.port);%0A    %7D%0A%0A    /**%0A     * Read a single line from a bare InputStream.%0A     *%0A     * @return The line.s%0A     */%0A    protected String readLine(InputStream input) throws IOException %7B%0A        BufferedReader reader = new BufferedReader(new InputStreamReader(%0A            input));%0A        return reader.readLine();%0A    %7D%0A%0A    /**%0A     * Retrieve the IP address for a given domain from the NS server.%0A     *%0A     * @param domain%0A     * @return Raw server response.%0A     */%0A    public String resolve(String domain)%0A            throws IOException, UnknownHostException %7B%0A        this.connect();%0A%0A        OutputStream request = this.sock.getOutputStream();%0A        request.write((domain + %22%5Cn%22).getBytes());%0A%0A        return this.readLine(this.sock.getInputStream());%0A    %7D%0A%0A    /**%0A     * Exit the client.%0A     */%0A    public void exit() %7B%0A        if (this.sock != null && !this.sock.isClosed()) %7B%0A            try %7B%0A                this.sock.close();%0A            %7D catch (Exception e) %7B%0A                System.err.println(%22Socket Error: %22 + e.getMessage());%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Run the client.%0A     */
 %0A   
@@ -2317,16 +2317,33 @@
 println(
+%0A                
 %22No doma
@@ -2514,12 +2514,654 @@
 %E2%80%A6%22);
+%0A%0A        Client client = new Client(%22localhost%22, 6052);%0A        try %7B%0A            String ip = client.resolve(domain);%0A            // send output to stderr if resolution failed%0A            if (ip.matches(IP_REGEX)) %7B%0A                System.out.println(ip);%0A            %7D else %7B%0A                System.err.println(ip);%0A            %7D%0A        %7D catch (UnknownHostException e) %7B%0A            System.err.println(%0A                %22Could not resolve name server hostname: %22 + e.getMessage());%0A        %7D catch (IOException e) %7B%0A            System.err.println(%22Socket Error: %22 + e.getMessage());%0A        %7D finally %7B%0A            client.exit();%0A        %7D
 %0A    %7D%0A%7D
@@ -3160,8 +3160,9 @@
 %0A    %7D%0A%7D
+%0A

implement client