Question: In this assignment, you're tasked with writing the server-side software for a simple guessing game. Clients will connect to your server software and guess a

In this assignment, 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.

This application is server-authoritative. This means that the correct answer should never be explicitly sent to or known by 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 port 7777.

Please use java socket programming for the sever class.

The client software has already been written. The code file can be found below:

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 { 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 != "hello ") { 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 = "quit "; } 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("guess\t{0} ", input); } else { // If the user entered anything else, make them to try again 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 == "correct ") { 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 // 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 } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!