Question

In: Computer Science

For this assignment you are to write both the server and client applications for a Knock-Knock...

For this assignment you are to write both the server and client applications for a Knock-Knock joke system based on the Java TCP socket client-server example that has been discussed in lectures. The joke protocol goes like this:

Client: "Tell me a joke."
Server: "Knock knock!"
Client: "Who's there?"
Server: "Witches."
Client: "Witches who?"
Server: "Witches the way to the store."
Client: "Groan."

The Client

The client code should connect to the joke server and allow the user to type in responses, like in the above example. To initiate the joke server, an initial request line must be fed to the server. The input sentences are sent to the server and processed and responded to by the server. The client code should then print the server response and allow for the next input from the user. Once the punchline has been delivered, the user will react and the client code will terminate the connection and the application.

The Server

The server code will wait for a client to connect on the designated port. When a client connection is made and upon receipt of an acceptable initial request, the server will make an initial response of "Knock, Knock!" to the client. Your server should have at least 5 jokes stored up (pairs of names and corresponding punchlines) and randomly choose one to feed to the client when the appropriate request line is received from the client. Once the punchline is delivered, a reaction from the client is accepted and the server is then able to terminate the connection and wait for a new client request.

If the expected response is not given, an appropriate error/prompting message is to be sent back to the client app in order to get the user running the client to put in the expected next line of the joke protocol. For debugging and/or logging purposes, the server should, at least, print to the console some indication of which joke has been selected.

Example:

Server: "Knock knock!"
Client: "What do you want?"
Server: "No, no, you're supposed to say 'Who's there?'"

Solutions

Expert Solution

Here is the source code for the socket programming done in Java language:

Server.java

import java.net.*; 
import java.io.*; 

public class Server 
{ 
        private Socket socket = null; 
        private ServerSocket server = null; 
        private DataInputStream in       = null; 

        public Server(int port) 
        { 
                try
                { 
                        server = new ServerSocket(port); 
                        System.out.println("Server has been started"); 
                        System.out.println("Waiting for the client ..."); 

                        socket = server.accept(); 
                        System.out.println("Client has accepted"); 

                        in = new DataInputStream( 
                                new BufferedInputStream(socket.getInputStream())); 

                        String line = ""; 
                        while (!line.equals("Is over.")) 
                        { 
                                try
                                { 
                                        line = in.readUTF(); 
                                        System.out.println(line); 

                                } 
                                catch(IOException i) 
                                { 
                                        System.out.println(i); 
                                } 
                        } 
                        System.out.println("Closing the connection"); 

                        socket.close(); 
                        in.close(); 
                } 
                catch(IOException i) 
                { 
                        System.out.println(i); 
                } 
        } 

        public static void main(String args[]) 
        { 
                Server server = new Server(6000); 
        } 
} 

Client.java

import java.net.*; 
import java.io.*; 

public class Client 
{ 
        private Socket socket = null; 
    private BufferedReader input = null;
        private DataOutputStream out = null; 

        public Client(String address, int port) 
        { 
                try
                { 
                        socket = new Socket(address, port); 
                        System.out.println("Connected Successfully"); 
                        input = new BufferedReader(new InputStreamReader(System.in)); 
                        out = new DataOutputStream(socket.getOutputStream()); 
                } 
                catch(UnknownHostException u) 
                { 
                        System.out.println(u); 
                } 
                catch(IOException i) 
                { 
                        System.out.println(i); 
                } 

                String line = ""; 
                while (!line.equals("Over")) 
                { 
                        try
                        { 
                                line = input.readLine(); 
                                out.writeUTF(line); 
                        } 
                        catch(IOException i) 
                        { 
                                System.out.println(i); 
                        } 
                } 

                try
                { 
                        input.close(); 
                        out.close(); 
                        socket.close(); 
                } 
                catch(IOException i) 
                { 
                        System.out.println(i); 
                } 
        } 

        public static void main(String args[]) 
        { 
                Client client = new Client("127.0.0.1", 6000); 
        } 
} 

Output:


Related Solutions

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 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...
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...
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk...
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk to a server program in two separate terminals. Write the server program that can handle multiple clients (so threads will be needed) and with fork() and exec()
Instructions Write a Client/Server application to do the following​ Client​ Take in IP address and Port...
Instructions Write a Client/Server application to do the following​ Client​ Take in IP address and Port as command line arguments​ Connect to Server​ Start "infinite loop"​ Take in message from command line​ Convert it to the packet form from last lab​ Instead of an array, store packet in a char array (string) so it can be easily sent​ Print out packet to terminal​ Send packet to server​ Print out reply from server End "infinite loop" Server Create connection on the...
Instructions Write a Client/Server application to do the following​ Client​ Take in IP address and Port...
Instructions Write a Client/Server application to do the following​ Client​ Take in IP address and Port as command line arguments​ Connect to Server​ Start "infinite loop"​ Take in message from command line​ Convert it to the packet form from last lab​ Instead of an array, store packet in a char array (string) so it can be easily sent​ Print out packet to terminal​ Send packet to server​ Print out reply from server End "infinite loop" Server Create connection on the...
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...
write an assignment about: Basic Methods of Collecting Data for engineering applications
write an assignment about: Basic Methods of Collecting Data for engineering applications
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT