Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java; Implement the knock knock joke message exchange pattern using a client-server architecture and Java sockets (java.net.Socket and java.net.ServerSocket). Server Set-up Details -Your server

Using Java;

Implement the knock knock joke message exchange pattern using a client-server architecture and Java sockets (java.net.Socket and java.net.ServerSocket).

Server Set-up Details

-Your server shall read three command line arguments: an integer TCP port, the name of a text file containing several knock-knock jokes, and a long seed value for a random number generator to select the next joke.

-Your server shall check that the correct number of arguments have been entered on the command line. Also, check that the numbers are parsable (java.lang.Integer.parseInt(String) and java.lang.Long.parseLong(String)) and that the jokes file exists (java.io.FileReader(String)). If any errors with command line arguments exists, catch the appropriate exceptions, print a helpful message to the console, and exit.

-Each line in the jokes input file has a joke setup and a punchline separated by a # character. Read all of the lines from the jokes input file into an appropriate data structure that can be indexed by an integer.

-You may find the java.lang.String.split(regex) method helpful in parsing each line in the file. Your server should bind to its port and print a message to the console indicating on which port the server is listening. The server should print the clients host name and port. The server only needs to handle one client at a time (a single-threaded server). The server should set up a java.io.PrintWriter and a java.io.BufferedReader so it can read messages from and write messages to the client.

-For the final step in preparing for the Knock Knock message exchange is for the server to obtain a random index to the data structure and retrieve the set-up and punchline to a joke. See java.util.Random(long) and java.util.Random.nextInt(int). Be sure your server implementation only instantiates a single Random object.

Client Set-up Details

-Your client shall read two command line arguments: the name or the IP address of the server and the servers TCP port.

-Your client shall check that the correct number of command line arguments have been entered. Also, check that the port number is parsable. If any errors exists with command line arguments exists, catch an appropriate exception, print a helpful error message, and exit.

-Your client should connect to the server using the entered server host and port. The client should setup a java.io.PrintWriter and a java.io.BufferedReader so it can read messages from and write messages to the client.

Message Exchange Details

-Your client and server should exchange messages following the Knock Knock message exchange pattern seen below. The pattern starts with the client sending a request message, Tell me a joke. The pattern ends with the server sending a Bye. message. The client and server shall each echo to their respective consoles every message sent to and received from their sockets. After the server sends a Bye. message it shall close its connection with the current client and wait to accept a client from another connection.

Client: Tell me a joke.

Server: Knock knock.

Client: Whos there?

Server: [set-up line]

Client: [set-up line] who?

Server: [punch line]

Server: Bye.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

How should instructions be written? Give a brief original example

Answered: 1 week ago

Question

Question What are the advantages of a written bonus plan?

Answered: 1 week ago