//Send both players their scores. public static void SendScores() { for (int i = 0; i < 2; i++) { Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ServerSend.SendToClient(Globals.players[i].Ip, (Globals.player1Score + "$" + Globals.player2Score + "#"), socketSend); socketSend.Close(); } }
//Start the game when both players are connected public static void GameStart() { //Check is both players are in list of players if (Globals.players.Count == 2) { for (int i = 0; i < 2; i++) { Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ServerSend.SendToClient(Globals.players[i].Ip, "Game Start!" + Globals.players[0].Name + "@" + Globals.players[1].Name + "%", socketSend); socketSend.Close(); } } }
//Sends generated letter when it is the corresponding players turn to choose. public static void SendGeneratedStringOfLetters() { String playflag = ""; int play = Globals.count % 2; for (int i = 0; i < 2; i++) { playflag = "!wait!"; Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Send to player 1 if (i == 0) { if (play == 0) { playflag = "!play!"; ServerSend.SendToClient(Globals.players[i].Ip, (Globals.listOfLetters + "#" + playflag), socketSend); } else { ServerSend.SendToClient(Globals.players[i].Ip, (Globals.listOfLetters + "#" + playflag), socketSend); } } //Send to player 2 if (i == 1) { if (play == 1) { playflag = "!play!"; ServerSend.SendToClient(Globals.players[i].Ip, (Globals.listOfLetters + "#" + playflag), socketSend); } else { ServerSend.SendToClient(Globals.players[i].Ip, (Globals.listOfLetters + "#" + playflag), socketSend); } } socketSend.Close(); } }
void ReceivedByServer() { Socket socketReceive = CreateSocketServer.ReceiveSocket(); while (true) { //Create temporary socket to receive messages from clients Socket temp = null; try { temp = socketReceive.Accept(); byte[] messageReceivedByServer = new byte[100]; int sizeOfReceivedMessage = temp.Receive(messageReceivedByServer, SocketFlags.None); string str = Encoding.ASCII.GetString(messageReceivedByServer); CheckIP.CheckIpAddress(str); //Display message received from clients in server label. labelShow.Text += "\r\n Client: " + str; Console.WriteLine("Server element after P :" + str.ElementAt(str.IndexOf("^") + 2)); if (Globals.players.Count < 2) { Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ServerSend.SendToClient(Globals.players[0].Ip, "Wait for the other player", socketSend); } if (Globals.count == 2) { PlayGame.GameStart(); } if (Globals.count > 2 && Globals.count < 13) { PlayGame.SendGeneratedStringOfLetters(); } if (Globals.count > 12 && Globals.count < 15) { string found = CheckWord.CheckExistingWord(str); //contains false send word does not exist Console.WriteLine(found.ToString()); if (found.Contains("false")) { int endPos = found.IndexOf("e"); string userIP = ""; Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); for (int i = endPos + 1; i < found.Length; i++) { userIP += found.ElementAt(i); } ServerSend.SendToClient(userIP, "!Invalid!", socketSend); } if (Globals.count == 14) { PlayGame.SendScores(); } } if (str.Contains("!Reset!")) { Globals.count = 2; Globals.listOfLetters = ""; PlayGame.SendGeneratedStringOfLetters(); for (int i = 0; i < 2; i++) { Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string catchedMsg = ServerSend.SendToClient(Globals.players[i].Ip, "!Reset!", socketSend); socketSend.Close(); } } if (str.ElementAt(str.IndexOf("#") + 1).Equals('4')) { if (str.ElementAt(str.IndexOf("^") + 2).Equals('1')) { Console.WriteLine("Server element after P :" + str.ElementAt(str.IndexOf("^") + 2)); Globals.P1Played = true; } if (str.ElementAt(str.IndexOf("^") + 2).Equals('2')) { Globals.P2Played = true; } if (Globals.P1Played && Globals.P2Played) { for (int i = 0; i < 2; i++) { Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string catchedMsg = ServerSend.SendToClient(Globals.players[i].Ip, "!DisplayScore!", socketSend); Console.WriteLine(catchedMsg); socketSend.Close(); } } } if (str.Contains("RestartServer")) { for (int i = 0; i < 2; i++) { Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string catchedMsg = ServerSend.SendToClient(Globals.players[i].Ip, "!RestartClient!", socketSend); Console.WriteLine(catchedMsg); socketSend.Close(); } Globals.ResetGlobals(); } } catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.StackTrace + "\n" + ex.HelpLink + "\n" + ex.InnerException + "\n" + ex.Source + "\n" + ex.TargetSite); } finally { temp.Close(); } } }