Update the Assignment 3 code to follow the guidelines below it.
Purpose of Assignment 3:
In assignment 3, you're tasked with writing the server-side software for a simple guessing game.
Clients will connect to your server software and guess a random number between 1-99 (inclusive).
If they guess correctly, they receive a congratulations message. If not, they'll be told whether their
guess was too high or too low. They should also have the option to disconnect from the server at
any time.
MY SERVER FOR ASSIGNMENT 3:
package SocketProgramming;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class Assignment3 {
public static void main(String[] args) {
// Create a TCP stream socket bound to 127.0.0.1 and port 7777 to listen for incoming clients
try {
InetAddress ipAddress = InetAddress.getByName("127.0.0.1");
try (ServerSocket serverSocket = new ServerSocket(7777, 0, ipAddress)) {
// Loop forever to listen for connections
while (true) {
// Accept an incoming connection, creating a socket for that client
Socket clientSocket = serverSocket.accept();
// Generate a random integer from 1 to 99 code(inclusive)
Random randomNum = new Random();
int answer = randomNum.nextInt(99) + 1;
// Send a 'hello' message to the client
String message = "hellorn";
OutputStream outputStream = clientSocket.getOutputStream();
outputStream.write(message.getBytes());
// Loop forever to receive new messages from this client
while (true) {
// Receive an incoming message and decode it using ASCII encoding
InputStream inputStream = clientSocket.getInputStream();
byte[] buffer = new byte[1024];
int numBytesReceived = inputStream.read(buffer);
String textReceived = new String(buffer, 0, numBytesReceived, "ASCII");
// If the message is a 'quit' message, disconnect from this client and start listening again
if (textReceived.equals("quitrn")) {
clientSocket.close();
break;
}
// If the message is a 'guess' message, parse the client's guess, then...
else if (textReceived.startsWith("guesst")) {
String guessString = textReceived.substring(6);
// ...If their guess matches the answer, send a 'correct' message to them and disconnect from the
client
if (Integer.parseInt(guessString) == answer) {
String response = "correctrn";
outputStream.write(response.getBytes());
clientSocket.close();
break;
}
// ...If their guess is greater than the answer, send a 'too-high' message and continue listening
else if (Integer.parseInt(guessString) > answer) {
String response = "too-highrn";
outputStream.write(response.getBytes());
}
// ...If their guess is less than the answer, send a 'too-low' message and continue listening
else {
String response = "too-lowrn";
outputStream.write(response.getBytes());
}
}
}
}
}
} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
THE CLIENT FOR ASSIGNMENT 3:
using System;
using System.Net;
using System.Net.Socket.
Update the Assignment 3 code to follow the guidelines below .pdf
1. Update the Assignment 3 code to follow the guidelines below it.
Purpose of Assignment 3:
In assignment 3, you're tasked with writing the server-side software for a simple guessing game.
Clients will connect to your server software and guess a random number between 1-99 (inclusive).
If they guess correctly, they receive a congratulations message. If not, they'll be told whether their
guess was too high or too low. They should also have the option to disconnect from the server at
any time.
MY SERVER FOR ASSIGNMENT 3:
package SocketProgramming;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class Assignment3 {
public static void main(String[] args) {
// Create a TCP stream socket bound to 127.0.0.1 and port 7777 to listen for incoming clients
try {
InetAddress ipAddress = InetAddress.getByName("127.0.0.1");
try (ServerSocket serverSocket = new ServerSocket(7777, 0, ipAddress)) {
// Loop forever to listen for connections
while (true) {
// Accept an incoming connection, creating a socket for that client
Socket clientSocket = serverSocket.accept();
// Generate a random integer from 1 to 99 code(inclusive)
Random randomNum = new Random();
int answer = randomNum.nextInt(99) + 1;
// Send a 'hello' message to the client
String message = "hellorn";
OutputStream outputStream = clientSocket.getOutputStream();
outputStream.write(message.getBytes());
// Loop forever to receive new messages from this client
while (true) {
// Receive an incoming message and decode it using ASCII encoding
InputStream inputStream = clientSocket.getInputStream();
byte[] buffer = new byte[1024];
int numBytesReceived = inputStream.read(buffer);
String textReceived = new String(buffer, 0, numBytesReceived, "ASCII");
// If the message is a 'quit' message, disconnect from this client and start listening again
if (textReceived.equals("quitrn")) {
clientSocket.close();
2. break;
}
// If the message is a 'guess' message, parse the client's guess, then...
else if (textReceived.startsWith("guesst")) {
String guessString = textReceived.substring(6);
// ...If their guess matches the answer, send a 'correct' message to them and disconnect from the
client
if (Integer.parseInt(guessString) == answer) {
String response = "correctrn";
outputStream.write(response.getBytes());
clientSocket.close();
break;
}
// ...If their guess is greater than the answer, send a 'too-high' message and continue listening
else if (Integer.parseInt(guessString) > answer) {
String response = "too-highrn";
outputStream.write(response.getBytes());
}
// ...If their guess is less than the answer, send a 'too-low' message and continue listening
else {
String response = "too-lowrn";
outputStream.write(response.getBytes());
}
}
}
}
}
} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
THE CLIENT FOR ASSIGNMENT 3:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
// Written in C# using .NET 7.0
// Note: This program intentionally does not perform exception handling
// This will allow you to more easily diagnose issues with your
connection and protocol
public static class Assignment3 {
3. public static void Main() {
Client();
}
private static void Client() {
// Create a TCP stream socket local to this computer using port 7777
var ipAddress = IPAddress.Parse("127.0.0.1");
var remoteEp = new IPEndPoint(ipAddress, 7777);
using var sender = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
sender.Connect(remoteEp);
// The server must initially send the client a 'hello' message upon
connection
var buffer = new byte[1024];
var numBytesReceived = sender.Receive(buffer);
var textReceived = Encoding.ASCII.GetString(buffer, 0,
numBytesReceived);
// If the server does not respond with the 'hello' message, terminate
the program
if (textReceived != "hellorn") {
Console.WriteLine("Server is not ready yet");
return;
}
// Loop forever to receive user input
for (; ; ) {
// Allow the user to guess a number, or an empty string to quit
Console.Write("Enter your guess (leave empty to disconnect): ");
var input = Console.ReadLine();
string message;
if (string.IsNullOrWhiteSpace(input)) {
// If the user's input was empty, our message to the server
will be a 'quit' message
input = null;
message = "quitrn";
}
else if (int.TryParse(input, out int guess)) {
// If the user entered a valid integer, our message will be
a 'guess' message
message = string.Format("guesst{0}rn", input);
}
else {
// If the user entered anything else, make them to try
again
4. Console.WriteLine("Invalid input");
continue;
}
// Send the ASCII encoded message to the server
var bytes = Encoding.ASCII.GetBytes(message);
sender.Send(bytes);
// If the user didn't enter anything, the client can safely
disconnect and end the program
if (input == null) {
break;
}
// Receive a response from the server and decode it using ASCII
encoding
numBytesReceived = sender.Receive(buffer);
textReceived = Encoding.ASCII.GetString(buffer, 0,
numBytesReceived);
// If the server responded with a 'correct' message, tell the
user they won and stop the loop
if (textReceived == "correctrn") {
Console.WriteLine("Congratulations! You won!");
break;
}
// The server should have responded with either a 'too-high' or
'too-low' message as a hint
else {
Console.WriteLine("Incorrect guess. Here's a hint: {0}",
textReceived.TrimEnd());
}
}
// Shut down the connection to the server
sender.Shutdown(SocketShutdown.Both);
}
private static void Server() {
throw new NotImplementedException("Create the server software by
following these steps for Assignment 3");
// Create a TCP stream socket bound to 127.0.0.1 and port 7777 to
listen for incoming clients
// Loop forever to listen for connections
// Accept an incoming connection, creating a socket for that client
// Generate a random integer from 1 to 99 (inclusive)
// Send a 'hello' message to the client
// Loop forever to receive new messages from this client
5. // Receive an incoming message and decode it using ASCII
encoding
// If the message is a 'quit' message, disconnect from this
client and start listening again
// If the message is a 'guess' message, parse the client's
guess, then...
// ...If their guess matches the answer, send a
'correct' message to them and disconnect from the client
// ...If their guess is greater than the answer, send a
'too-high' message and continue listening
// ...If their guess is less than the answer, send a
'too-low' message and continue listening
}
}
WHAT I NEED TO DO:
In this assignment, you're tasked with updating your client/server program from Assignment 3 to
include server-side records. This is a common practice for client/server applications since both
clients and the server can benefit from record-keeping. Servers have a reliable log of client activity
while clients can access useful information stored on the trusted server. This program should have
the same functionality as Assignment 3 with added features related to record keeping. Unlike
Assignment 3, you will have to write your own client software in addition to the server software. If
you think it will help, you're welcome to use the provided client code from Assignment 3 as a
starting point. You are allowed and encouraged to re-use any code you wrote for Assignment 3.1.
The client should prompt the user for their desired username before initiating any connection with
the server. Make sure to trim whitespace from the name. You're welcome to impose other
restrictions on usernames if you'd like (hint: maybe disallow commas if you're using CSV
formatting on the server!). 2. The server should maintain an external file (txt, csv, etc.) which
contains each user's 'high score' for the guessing game. For this exercise, we consider users to be
identifiable solely by their username from Task 1. Their 'high score' is the minimum number of
guesses taken to win the guessing game. 3. When a client connects to the server, the server
should send it a 'hello' message (same as Assignment 3). The client should respond with a 'user'
message which contains their desired username. The server should respond with a 'welcome'
message which contains either this user's current high score or -1 if the server doesn't recognize
this user. Any deviation from this initial handshake should result in the client's disconnection from
the server. 4. When the client receives a 'welcome' message, parse the score argument. If it's -1 ,
print a message to the console welcoming the client to the game for the first time. Otherwise,
welcome them back and print what their previous high score was. 5. When the server sends a
'correct' message to a client, update the record file if necessary. No update is required if that user
did not beat their high score. 6. When the client receives a 'correct' message, print a message to
the console either congratulating them on setting a new high score, beating their previous high
score, or encouraging them to try again to beat their high score (all clients should fall into one of
these three categories) Note: When the server receives a 'quit' message from a first-time client,
6. you can either choose to 1) Not enter that client into the file at all, since they didn't finish a game,
or 2) enter something into your file to denote that the user quit, then modify Task III to handle the
case of known clients who don't have a high score ASCII Encoding should be used for all
messages|Server -> Client: Client - Server: This application is server-authoritative. This means
that the correct answer should never be explicitly sent to the client. The server should determine if
a client's guess is correct or not and send the appropriate response. You can use your computer's
local IP address 127.0.0.1 and the port 7777. Make sure your server software is running before
you try testing your client software, otherwise it won't have anything to connect to! Check your
firewall settings if the connection still cannot be made.