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.
Program description is given below!! Client and Server Online Game Jumble: In this assignment, you need...
Program description is given below!! Client and Server Online Game Jumble: In this assignment, you need to use socket programming to implement an online game Jumble. Generate a random work in server and send it to client and ask for input and then client send the guessed word to server and server check whether its in a word.txt file and reply Win or lose. Note: Reading content of a text file to a list and generating random number, and game...
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”
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
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...
Write and test a python program to demonstrate TCP server and client connections. The client sends...
Write and test a python program to demonstrate TCP server and client connections. The client sends a Message containing the 10 float numbers (FloatNumber[]) , and then server replies with a message containing maximum, minimum, and average of these numbers.
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...
Write a Python Client/Server Socket Program that will allow you to send text messages between the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the server and client, terminating when the exit is typed on the client. Build the program on your 2-VM’s and get it working. Cut and paste your code below along with screen shots of your program working.
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()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT