Question

In: Computer Science

For the assignment you have to do two thing Passing arguments when running the jar or...

For the assignment you have to do two thing

  • Passing arguments when running the jar or class file
    • To run the client, command to run from CMD will be: java –jar Client.jar <server-IP> <server-Port> OR java Client <server-IP> <server-Port>
    • To run the server, command to run from CMD will be: java –jar Server.jar <server-Port> <dns-table-file-name> OR java Server <server-Port> <dns-table-file-name>

  • In the server, Read the file and search for specific IP or URL based on the command received from client and reply the client with appropriate answer by adding appropriate code in the Server.java file.

.................................

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) throws IOException {

String serverHostname = new String ("127.0.0.1");

if (args.length > 0) {

//pass the hsotname through cmd argument

serverHostname = args[0];

}

System.out.println ("Attemping to connect to host " + serverHostname + " on

port 10007.");

Socket echoSocket = null;

PrintWriter out = null;

BufferedReader in = null;

try {

//Connect to server and open IO stream

echoSocket = new Socket(serverHostname, 10007);

out = new PrintWriter(echoSocket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(

echoSocket.getInputStream()));

} catch (UnknownHostException e) {

System.err.println("Don't know about host: " + serverHostname);

System.exit(1);

} catch (IOException e) {

System.err.println("Couldn't get I/O for " + "the connection to: " +

serverHostname);

System.exit(1);

}

BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in));

String userInput;

System.out.print ("input-comand: ");

while ((userInput = stdIn.readLine()) != null) {

//send to server using socket

out.println(userInput);

//read reply from server

String replyFromServer = in.readLine();

System.out.println("Answer: " + replyFromServer);

if(replyFromServer.trim().toLowerCase().equals("bye")){

break;

}

System.out.print ("input-command: ");

}

out.close();

in.close();

stdIn.close();

echoSocket.close();

}

}

--------------------------------------------

import java.net.*;

import java.io.*;

public class Server {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;

try {

serverSocket = new ServerSocket(10007);

}

catch (IOException e) {

System.err.println("Could not listen on port: 10007.");

System.exit(1);

}

Socket clientSocket = null;

System.out.println ("Waiting for connection ...");

try {

clientSocket = serverSocket.accept();

}

catch (IOException e) {

System.err.println("Accept failed.");

System.exit(1);

}

System.out.println ("Connection successful");

System.out.println ("Waiting for input ...");

//open IO streams

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader( clientSocket.getInputStream()));

String inputLine;

//wait and read input from client. Exit when input from socket is Bye

while ((inputLine = in.readLine()) != null)

{

System.out.println ("Server: " + inputLine);

//reply back to client

out.println(inputLine);

if (inputLine.trim().toLowerCase().equals("bye"))

break;

}

//close IO streams and at the end socket

out.close();

in.close();

clientSocket.close();

serverSocket.close();

}

}

---------------------------------

DNS-table.txt

www.google.com 192.168.73.1

www.uncc.edu 192.168.73.2

www.abc.com 192.168.73.3

www.espn.com 192.168.73.4

www.cyberDNA.com 192.168.73.55

www.ccaa.uncc.edu 192.168.73.5

www.ccigrad.uncc 192.168.73.13

www.cis.uncc.edu 192.168.73.14

www.unc.edu 192.168.73.12

Solutions

Expert Solution

//first run Server then client....

//Java code

import java.io.*;

import java.net.*;

public class Client {

    public static void main(String[] args) throws IOException {

        String serverHostname = new String ("127.0.0.1");

        if (args.length > 0) {

//pass the hsotname through cmd argument

            serverHostname = args[0];

        }

        System.out.println ("Attemping to connect to host " + serverHostname + " on port 10007.");

        Socket echoSocket = null;

        PrintWriter out = null;

        BufferedReader in = null;

        try {

//Connect to server and open IO stream

            echoSocket = new Socket(serverHostname, 10007);

            out = new PrintWriter(echoSocket.getOutputStream(), true);

            in = new BufferedReader(new InputStreamReader(

                    echoSocket.getInputStream()));

        } catch (UnknownHostException e) {

            System.err.println("Don't know about host: " + serverHostname);

            System.exit(1);

        } catch (IOException e) {

            System.err.println("Couldn't get I/O for " + "the connection to: " +

                    serverHostname);

            System.exit(1);

        }

        BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in));

        String userInput;

        System.out.print ("input-comand: ");

        while ((userInput = stdIn.readLine()) != null) {

//send to server using socket

            out.println(userInput);

//read reply from server

            String replyFromServer = in.readLine();

            System.out.println("Answer: " + replyFromServer);

            if(replyFromServer.trim().toLowerCase().equals("bye")){

                break;

            }

            System.out.print ("input-command: ");

        }

        out.close();

        in.close();

        stdIn.close();

        echoSocket.close();

    }

}

//=================================================

import java.net.*;

import java.io.*;
import java.util.Scanner;

public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;

        try {

            serverSocket = new ServerSocket(10007);

        }

        catch (IOException e) {

            System.err.println("Could not listen on port: 10007.");

            System.exit(1);

        }

        Socket clientSocket = null;

        System.out.println ("Waiting for connection ...");

        try {

            clientSocket = serverSocket.accept();

        }

        catch (IOException e) {

            System.err.println("Accept failed.");

            System.exit(1);

        }

        System.out.println ("Connection successful");

        System.out.println ("Waiting for input ...");

//open IO streams

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

        BufferedReader in = new BufferedReader(new

                InputStreamReader( clientSocket.getInputStream()));

        String inputLine;

//wait and read input from client. Exit when input from socket is Bye
        Scanner scanFile = new Scanner(new File("DNS-table.txt"));
        boolean found =false;
        while ((inputLine = in.readLine()) != null)

        {

            System.out.println ("Server: " + inputLine);
            String line ="";

            if (inputLine.trim().toLowerCase().equals("bye"))
            {
                out.println(inputLine);
                break;
            }


            while (scanFile.hasNextLine())
            {
                //reply back to client
                line  = scanFile.nextLine();
                String[] tokens = line.split(" ");
                if(inputLine.equalsIgnoreCase(tokens[0]) || inputLine.equalsIgnoreCase(tokens[1])) {
                    found = true;
                    break;
                }

                else
                    found = false;

            }
            if(found) {
                out.println(line);

            }
            else {


                out.println("Not found...");
            }
        }


//close IO streams and at the end socket
        scanFile.close();
        out.close();

        in.close();

        clientSocket.close();

        serverSocket.close();

    }

}

//text file

//output

//If you need any help regarding this solution ........... please leave a comment .......... thanks


Related Solutions

Precis 3 To complete this assignment, follow the instructions below. You will have two arguments to...
Precis 3 To complete this assignment, follow the instructions below. You will have two arguments to address from both Chapter 8 and Chapter 9. From Chapter 8, Exercise 8.9, write a precis on passages 4 and 5. 4: If the Copernican and Darwinian theories are reasonable representatives of scientific revolutions, Sigmund Freud’s theory of psychology is a candidate for a revolution in thought. Thus, because both the Copernican and Darwinian theories are reasonable representatives of scientific revolutions, Freud’s theory is...
What is the FIRST thing you should do when you speak to this community about the...
What is the FIRST thing you should do when you speak to this community about the risk from the PFAS contamination? (select one option only) Discuss sources of scientific uncertainty Compare the magnitude of this risk to other risks Express empathy and understanding Present and explain the results of testing
WHAT DO YOU THINK IS THE MOST IMPORTANT THING NURSES NEED TO KEEP IN MIND WHEN...
WHAT DO YOU THINK IS THE MOST IMPORTANT THING NURSES NEED TO KEEP IN MIND WHEN DEALING WITH MENTAL HEALTH?
What do you think is the most important thing to keep in mind when applying realist...
What do you think is the most important thing to keep in mind when applying realist ideas to foreign policy analysis? Why? What could be the pitfalls?
You have two jars of m&m's. Jar 1 has a defined proportion of yellow and orange...
You have two jars of m&m's. Jar 1 has a defined proportion of yellow and orange m&m's (10% of each). Jar 2 is either 5% orange, 5% yellow OR 14% orange, 17% yellow. If you draw one M&M from jar 2, what is the probability that it is orange or yellow?
You have two jars of m&m's. Jar 1 has a defined proportion of yellow and orange...
You have two jars of m&m's. Jar 1 has a defined proportion of yellow and orange m&m's (10% of each). Jar 2 is either 5% orange, 5% yellow OR 14% orange, 17% yellow. If you draw one M&M from jar 2, what is the probability that it is orange or yellow?
Assignment instructions: In this assignment, you will first have to go “out” and do something before...
Assignment instructions: In this assignment, you will first have to go “out” and do something before you can write it up. Read here what you need to do: - Break a social norm (Caution: do not wear anything illegal, dangerous, or extremely disruptive.) For example: Wear formal attire to a casual get together. Wear a winter jacket in the summer. Wear sandals in the snow. Wear your pajamas outside. Wear an unstylish or outdated outfit. (What is a social norm?...
Do you think theres a such thing as american culture?
Do you think theres a such thing as american culture?
What are the Arguments for Protection? Define each of the arguments for protection. Do you believe...
What are the Arguments for Protection? Define each of the arguments for protection. Do you believe that the arguments for protection are necessary or unnecessary? Are some necessary? Explain your answers using current events.
You are running a nursery for garden peas at your university, and you have two pure-bred...
You are running a nursery for garden peas at your university, and you have two pure-bred garden pea strains: Yellow Wrinkled (YYrr) and Green Round (yyRR). You know that Yellow is dominant to green and that Round is dominant to Wrinked. One customer asked about one particular pure-bred strain, Yellow Round (YYRR). You promised to that customer that you could generate this pure-bred one. 1. Provide breeding schemes. 2. After several breeding experiments, you need to identify the pure-bred one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT