Question

In: Computer Science

In the provided client and server code, the server can serve to single client at a...

In the provided client and server code, the server can serve to single client at a time. You have to change server.java code so that it can connect and serve multiple clients at the same time. Use multithreading.
===============================================================================
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

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

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);
                }

                while (true) {
                        Socket clientSocket = null;
                        System.out.println("Waiting for connection ...");

                        try {
                                clientSocket = serverSocket.accept();
                        } catch (IOException e) {
                                System.err.println("Accept failed.");
                                System.exit(1);
                        }

                        new ConnectionHandler(clientSocket).start();
                }

        }

        static class ConnectionHandler extends Thread {
                private Socket clientSocket;
                public ConnectionHandler(Socket clientSocket) {
                        this.clientSocket = clientSocket;
                }

                public void run() {

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

                        // open IO streams
                        try {
                                PrintWriter out;
                                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();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
        }
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

what will be the code in C programming for the client and server chat application for...
what will be the code in C programming for the client and server chat application for the below issue :- write the C Programming code that able client have a unique ID to be known by the server
There are many factors that can influence such decision for cloud server and client server. security,...
There are many factors that can influence such decision for cloud server and client server. security, cost, training and more. which would you choose and why ? there are many factors that influenced the decision on a cloud server or client server such as cost, security, training and more. which one would you choose, cost, security,training etc. and why ? cancel that answer
The client connects to the server. The server accepts the connection. The client sends a line...
The client connects to the server. The server accepts the connection. The client sends a line of text containing only SHAKESPEARE_COUNTS. The server sends back a sequence of integers. The number of integers in that list specifies the number of words in each insult; the numbers themselves specify how many possible words there are in each position. So, for example, if you received the output 15 20 30, it would indicate that insults are three words long, with 15 possible...
in Java - implement ftp-server and ftp-client. ftp-server Logging into ftp-server from ftp-client The ftp-server is...
in Java - implement ftp-server and ftp-client. ftp-server Logging into ftp-server from ftp-client The ftp-server is an interactive, command-line program that creates a server socket, and waits for connections. Once connected, the ftp-client can send and receive files with ftp-server until ftp-client logs out. Sending and receiving files The commands sent from the ftp-client to the ftp-server must recognize and handle are these: rename- the ftp-server responds to this command by renaming the named file in its current directory to...
A limitation of the chat server is that it can handle only one client because it...
A limitation of the chat server is that it can handle only one client because it is a single threaded application. Using the pThread library, modify the chat server so that it can handle multiple clients simultaneously, i.e., by creating a new thread whenever a client is connected so that the client is handled individually with a new thread, and at the same time, by having the main thread (i.e., the thread that runs the main function) of the chat...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
1. A Client-side digital signature can be used to authenticate to a web server through SSL,...
1. A Client-side digital signature can be used to authenticate to a web server through SSL, but confidentiality can still be vulnerable to attack. Identify a type of attack to which the use of SSL is vulnerable and justify your answer. 2. Discuss the benefits of MPLS LSP (multiprotocol label switching label switched path) to support high availability of service with illustration of use for Push, Swap and Pop. 3. The manager wishes to access confidential company data while travelling...
In Simple Chat, if the server shuts down while a client is connected, the client does...
In Simple Chat, if the server shuts down while a client is connected, the client does not respond, and continues to wait for messages. Modify the client so that it responds to the shutdown of server by printing a message saying the server has shut down, and quitting. (look at the methods called connectionClosed and connectionException). //ChatClient.java // This file contains material supporting section 3.7 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source //...
Please code in C language. Server program: The server program provides a search to check for...
Please code in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
Develop a client /server talk application in C or C++. You should run your server program...
Develop a client /server talk application in C or C++. You should run your server program first, and then open another window if you use loopback to connect the local host again, or you may go to another machine to run the client program that will connect to the server program. In this case you must find the server’s IP address first, and then connect to it. If you use loopback, your procedure may look like: ➢ Server Server is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT