In: Computer Science
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?'"
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: