using System; using System.Text; using System.Threading.Tasks; using System.Net.Sockets; using System.Net; namespace Server { internal class Program { // Attributes // Port Number of the server. public const int PORT_NUMBER = 19000; // Define the buffer size. public const int BUFFER_SIZE = 100; // Server host name. public const string HOST_NAME = "localhost"; /// /// Setting up and establishing the server code. /// /// static void Main(string[] args) { // Determines the IP via the .GetHostAddresses IPAddress myIP = Dns.GetHostAddresses(HOST_NAME)[0]; TcpListener listener = new TcpListener(myIP, PORT_NUMBER); listener.Start(); // To test whether or not the server is able to start correctly, if there is an issue, this is caught in the catch block. try { // Start a listener which is used to catch socket exceptions. // This is where TCP will be used. Socket Listener = new Socket(SocketType.Stream, ProtocolType.Tcp); // The socket should be bound to the IP end point. // This is done for a maximum of 10 incomming connections. // if it is sucessful then report to the console that it was successfully started. listener.Start(); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("The Server has Sucessfully Started."); Console.ForegroundColor= ConsoleColor.Blue; Console.WriteLine("Host Name: " + HOST_NAME + " | Hosting on Port: " + PORT_NUMBER); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Server is now being hosted on the IP of: " + myIP); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("---------------------------------------------------------------------"); } // This is used to notify that the server was unable to start successfully. // Errors messages are used via Exception e, which are outputted to the console for the user to read. catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("The Server is unable to start sucessfully"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Type of error: " + e.GetType() + "Exception: " + (e.Message)); Console.ReadKey(); } /// /// Main Method of code which is used to carry out the game process. /// /// while (true) { // Waits for a client to join the server. TcpClient client = listener.AcceptTcpClient(); // Console writes a new line to show that the client has connected. Console.WriteLine($"Client has connected to the server."); // When the client joins the server, the guess a number task starts. Task.Run(() => { try { // Generates a random number from 1 to 50. Random rand = new Random(); int RandomNumber = rand.Next(1, 51); // Output to the console the number that has been generated. Console.WriteLine("The number that has been generated for the client to guess is: " + RandomNumber); // Guess the number message has been generated and is sent to the client. byte[] buffer = Encoding.ASCII.GetBytes("Guess the number between 1 and 50."); NetworkStream stream = client.GetStream(); stream.Write(buffer, 0, buffer.Length); while (true) { // Recieves the clients guess of the number that is between 1 and 50. // Message is buffered. buffer = new byte[BUFFER_SIZE]; int bytesRead = stream.Read(buffer, 0, buffer.Length); string ClientGuess = Encoding.ASCII.GetString(buffer, 0, bytesRead); // Logs the clients guess of the number and is stored into a string. Console.WriteLine("Received guess: " + ClientGuess); // Declare the string that is the clients number. int ClientGuessNumber; if (int.TryParse(ClientGuess, out ClientGuessNumber)) { // If the client guesses the number which is the same as the number that was generated between 1 and 50. if (ClientGuessNumber == RandomNumber) { // Send a message to the client that they were correct and that they have won the game. buffer = Encoding.ASCII.GetBytes("Correct! You win!"); stream.Write(buffer, 0, buffer.Length); break; } // If the number that the client has provided is lower than the number that was generated. else if (ClientGuessNumber < RandomNumber) { // Send a message to the client to highlight that the guess was too low, they will need to guess again. buffer = Encoding.ASCII.GetBytes("Too low. Guess again."); stream.Write(buffer, 0, buffer.Length); } // If the number that the client has provided is higher than the number that was generated. else { // Send a message to the client to highlight that the guess was too high, they will need to guess again. buffer = Encoding.ASCII.GetBytes("Too high. Guess again."); stream.Write(buffer, 0, buffer.Length); } } // If the user does not provide a relative number. else { // Send a message to the client that their input is incorrect/invalid. buffer = Encoding.ASCII.GetBytes("Invalid input. Please enter a number."); stream.Write(buffer, 0, buffer.Length); } } } // If an error is experienced in this section, display to the console that there is an error. // Exception ex, is used to provide an error message of the problem. catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } }); } } } }