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
Greetings, Consider a client server model.The server sends the message 'I am the server' to client....
Greetings, Consider a client server model.The server sends the message 'I am the server' to client. Describe and compare in details how the client and server exchange these messages using internet domain in the following two modes. a) connection-oriented modes b) connectionless-oriented modes
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.
In web programming what is the Client-Server model?
In web programming what is the Client-Server model?
In web programming what is the Client-Server model?
In web programming what is the Client-Server model?
Write two python codes for a TCP client and a server using a Socket/Server Socket. The...
Write two python codes for a TCP client and a server using a Socket/Server Socket. The client sends a file name to the server. The server checks if there is any integer value in the file content. If there is an integer number, the server will send a message to the client “Integer exists” otherwise, the message “Free of Integers”
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT