using System; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Text; using System.Threading; internal class Program { //hostname of the server public const string HOST_NAME = "server.allcatsaregrey.co.uk"; //port number public const int PORT_NUMBER = 26260; private static void Main(string[] args) { //generate random number Random random = new Random(); //start up the server try { //define the local IP address and port number - port 26260 IPAddress ipAddress = Dns.GetHostAddresses(HOST_NAME)[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, PORT_NUMBER); //create a new TCP/IP socket and bind it to the local endpoint Socket server = new Socket(SocketType.Stream, ProtocolType.Tcp); server.Bind(localEndPoint); server.Listen(1); //write stating that the server has started and there is no client yet Console.WriteLine("Server started. Waiting for a client..."); //wait for a client to connect Socket client = server.Accept(); //once they have connected state that the client has connected Console.WriteLine("Client connected."); //deal the first card to the player int playerCard1 = random.Next(1, 11); //deal the second card to the player int playerCard2 = random.Next(1, 11); //add the total of the cards to create a total int playerTotal = playerCard1 + playerCard2; //have the server write the cards that the player has and their total Console.WriteLine("Player's cards: {0}, {1}. Total: {2}", playerCard1, playerCard2, playerTotal); //send the player's total to the client byte[] playerTotalData = Encoding.ASCII.GetBytes(playerTotal.ToString()); client.Send(playerTotalData); //give the dealer their first card int dealerCard = random.Next(1, 11); int dealerTotal = dealerCard; Console.WriteLine("Dealer's card: {0}", dealerCard); //wait for the player to hit or stand while (playerTotal <= 21) { //wait for the client to send whether they have decided to hit or stand byte[] hitStandData = new byte[1024]; int bytesReceived = client.Receive(hitStandData); //close the server if there is no message if (bytesReceived == 0) { break; } string hitStand = Encoding.ASCII.GetString(hitStandData, 0, bytesReceived); //if the client has decided to hit deal them another card if (hitStand == "HIT") { //deal another card to the player int playerCard = random.Next(1, 11); //add it to their total playerTotal += playerCard; //have the server write the player's new total Console.WriteLine("Player hit. Card: {0}. Total: {1}", playerCard, playerTotal); //send the player's total to the client playerTotalData = Encoding.ASCII.GetBytes(playerTotal.ToString()); client.Send(playerTotalData); } //if the client has decided to stand then do not give them anymore cards else if (hitStand == "STAND") { while (dealerTotal < 17) { //give the dealer a new card int dealerNewCard = random.Next(1, 11); //add it to their total dealerTotal += dealerNewCard; //have the server write what the dealer has been given and their total Console.WriteLine("Dealer hit. Card: {0}. Total: {1}", dealerNewCard, dealerTotal); } //send the dealers total to the client byte[] dealersTotal = Encoding.ASCII.GetBytes(dealerTotal.ToString()); client.Send(dealersTotal); //code to determine the winner Console.WriteLine("Dealer stands. Player total: {0}. Dealer total: {1}", playerTotal, dealerTotal); //declare a new string string winner; //if the dealer's total amounts to more than 21, the winner is the player if (dealerTotal > 21 || playerTotal > dealerTotal) { winner = "PLAYER"; } //if the player and the dealer have the same total, then the game is declared a tie else if (playerTotal == dealerTotal) { winner = "TIE"; } //if the player has more than 21 then they have bust else if (playerTotal > 21) { winner = "DEALER, YOU BUST!"; } else { winner = "DEALER"; } //send the winner to the client byte[] winnerData = Encoding.ASCII.GetBytes(winner); client.Send(winnerData); break; } //check if the client is connected if (!client.Connected) { Console.WriteLine("Client has disconnected."); break; } } //close the connection and the server if the client disconnects Console.WriteLine("\nThe client has disconnected"); Console.WriteLine("The server will now stop"); Thread.Sleep(6000); client.Close(); server.Close(); Environment.Exit(0); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.ReadLine(); } }