Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A simple puzzle game using Sockets: MooNet Scenario Write a server program in java that allows a basic client to connect to the server using

A simple puzzle game using Sockets: MooNet
Scenario
Write a server program in java that allows a basic client to connect to the server using sockets. Once connected the server will invite the client to play a game of Bulls and Cows.
In this game the server asks the client to guess a four digit code. The code is always 1058. Each time that the user makes a guess the client sends to to the server and the server replies with how many of the guessed digits are in the correct position (bulls), and how many are present but not in the correct position (cows).
BEFORE YOU BEGIN, click here to view a video demonstration of how the program should work.
Remember, your code will need to allow this to be performed over a network connection, between two running programs, Server and BetterClient.
In words
When the client first connects, the server sends a greeting and asks for username, then password to be submitted separately. The user is given 5 attempts in total (username attempts + password attempts) to get these right. If they fail on the 5th attempt then the server disconnects from the client.
When the client first connects, the server sends a greeting and asks for username, then password to be submitted separately. The user is given 5 attempts in total to get these right. If they fail on the 5th attempt then the server disconnects from the client.
If both the username and password are correctly submitted then the server sends this welcome message to the client:
Welcome Daisy!
Let's play Bulls and Cows!
Guess my 4 digit code.
Enter 4 digits, separated by spaces:
The user should then enter a 4 digit code, with each digit separated by spaces, for example:
5031
The server checks the code. In this case the 0 is in the correct position, while the 5 and 1 belong in the code but are not in the right places. So the server sends the response:
1 bull and 2 cow.
If the input is not correctly formatted, such as having too few digits, using non-numerical characters or not having spaces between the digits, then the server should send a response that lets the user know:
Oops! You need to enter 4 digits separated by spaces.
Try again:
The server should keep accepting guesses until the user guesses correctly. If the user guessed correctly then the server should send this response:
You got it - goodbye!
Requirements: READ CAREFULLY!
All user input must be done through the client.
The server must send messages to the client. You must assume that the user cannot see any console output of the Server class.
When the client connects to the server, the server must greet them with the message: Greetings from MooNet!
The username must be Daisy (note the capital letter).
The password must be moo (note that it is all lower case).
The code must be 1058.
You are required to submit a single class named Server.java, and it must contain all your code, including the main method. Any other classes or classnames will be ignored and not used for marking, so be very careful with the spelling of the classname.
Your class must work with the unmodified BetterClient.java class, found in here. Do not attempt to submit a modified version of this client class, or any other client code! It will not be used for marking.
"import java.net.UnknownHostException;
import java.net.InetAddress;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
/* client sends messages to server and receives responses
this version allows the server to send an initial greeting
and send multiples lines in each response */
class BetterClient {
private static InetAddress server;
private static Socket socket;
private static BufferedReader in;
private static PrintWriter out;
private static BufferedReader user;
private static String fromServer, userInput;
private static String eor ="[EOR]"; // a code for end-of-response
private static String exitCommand = "EXIT";
private static void setup(String [] args){
// check arguments are correct
if (args.length !=2){
toConsole("Usage: java LineClient host port");
System.exit(0);
}
int port =0;
String host = null;
try {
host = args[0];
port = Integer.parseInt(args[1]);
}
catch( NumberFormatException nfex ){
toConsole("Bad port number");
System.exit(0);
}
try {
/* determine the address of the server and connect to it */
server = InetAddress.getByName(host);
socket = new Socket(server, port);
toConsole("Connected: "+ socket);
toConsole("
Type "+ exitCommand +" to disconnect
");
// get the input stream and attach to a buffered reader
in = new"

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_2

Step: 3

blur-text-image_3

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

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions