void DoServerStuff() { // Setup as server here IPAddress ipAd = IPAddress.Parse(NetBreakoutPongServer.Constants.SERVER_IP); TcpListener server = new TcpListener(ipAd, NetBreakoutPongServer.Constants.SERVER_PORT); server.Start(); NetBreakoutPongServer.ClassicPongGameData data = new NetBreakoutPongServer.ClassicPongGameData(); data.gameBall.Position = new NetBreakoutPongServer.Location(250, 300); data.gameBall.Radius = 50; data.myPaddle.Height = 40; data.myPaddle.Width = 150; data.myPaddle.Position = new NetBreakoutPongServer.Location(500, 580); data.oppPaddle.Height = 40; data.oppPaddle.Width = 150; data.oppPaddle.Position = new NetBreakoutPongServer.Location(300, 20); NetBreakoutPongServer.GameClient client = new NetBreakoutPongServer.GameClient(server); //Thread.Sleep(5000); client.SendData(data); client.Dispose(); }
static void Main(string[] args) { Console.WriteLine("Pong Server is starting up..."); // Setup as server here IPAddress ipAd = IPAddress.Parse(Constants.SERVER_IP); TcpListener server = new TcpListener(ipAd, Constants.SERVER_PORT); server.Start(); while (true) { // Connect Two Clients Console.WriteLine("Waiting for Player One to connect..."); PlayerOne = new GameClient(server); Console.WriteLine("Waiting for Player Two to connect..."); PlayerTwo = new GameClient(server); try { do { Console.WriteLine("Generating a Classic Pong Game..."); game = new ClassicPongGame(); // Send initial locations Console.WriteLine("Ready..."); PlayerOne.SendData(game.GetDataForP1()); PlayerTwo.SendData(game.GetDataForP2()); // wait for a sec before starting Console.WriteLine("Set..."); Thread.Sleep(1000); // As long as the game is not over, keep playing. Console.WriteLine("Go!"); while (!game.PlayerOneLost() && !game.PlayerTwoLost()) { // Move and update everything for player one POneKP = PlayerOne.GetKeypress(); game.POneUpdate(POneKP); PlayerOne.SendData(game.GetDataForP1()); Thread.Sleep(Constants.SLEEP_DELAY); // Move and update everything for player two PTwoKP = PlayerTwo.GetKeypress(); game.PTwoUpdate(PTwoKP); PlayerTwo.SendData(game.GetDataForP2()); Thread.Sleep(Constants.SLEEP_DELAY); } // Throw away these keypresses. Nobody loves them. PlayerOne.GetKeypress(); PlayerTwo.GetKeypress(); // Send final positions with gameOverInfo // PlayerOne ClassicPongGameData temp = game.GetDataForP1(); temp.ILost = game.PlayerOneLost(); temp.OppLost = game.PlayerTwoLost(); PlayerOne.SendData(temp); // PlayerTwo temp = game.GetDataForP2(); temp.ILost = game.PlayerTwoLost(); temp.OppLost = game.PlayerOneLost(); PlayerTwo.SendData(temp); pOneContinue = PlayerOne.RequestsContinue(); pTwoContinue = PlayerTwo.RequestsContinue(); PlayerOne.ApproveContinue(pOneContinue && pTwoContinue); PlayerTwo.ApproveContinue(pOneContinue && pTwoContinue); } while (pOneContinue && pTwoContinue); } catch (IOException e) { Console.WriteLine("Connection with client(s) was interrupted unexpectedly, restarting game."); } Thread.Sleep(100); if (PlayerOne != null) { PlayerOne.Dispose(); } if (PlayerTwo != null) { PlayerTwo.Dispose(); } } }