Answered step by step
Verified Expert Solution
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 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 attempts in total username attempts password attempts to get these right. If they fail on the th 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 attempts in total to get these right. If they fail on the th 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 digit code.
Enter digits, separated by spaces:
The user should then enter a digit code, with each digit separated by spaces, for example:
The server checks the code. In this case the is in the correct position, while the and belong in the code but are not in the right places. So the server sends the response:
bull and cow.
If the input is not correctly formatted, such as having too few digits, using nonnumerical 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 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
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.ioPrintWriter;
import java.ioBufferedReader;
import java.ioOutputStream;
import java.ioInputStream;
import java.ioInputStreamReader;
import java.ioIOException;
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 endofresponse
private static String exitCommand "EXIT";
private static void setupString args
check arguments are correct
if argslength
toConsoleUsage: java LineClient host port";
System.exit;
int port ;
String host null;
try
host args;
port Integer.parseIntargs;
catch NumberFormatException nfex
toConsoleBad port number";
System.exit;
try
determine the address of the server and connect to it
server InetAddress.getByNamehost;
socket new Socketserver port;
toConsoleConnected: 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started