public ClientProcessor(GameState gameState, int id, TcpClient client)
        {
            this.gameState = gameState;
            this._id = id;
            this.client = client;

            NetworkStream stream = client.GetStream();
            reader = new StreamReader(stream);
            writer = new StreamWriter(stream);
        }
示例#2
0
        static void Main(string[] args)
        {
            TcpListener listener = null;
            int port = 50000;
            GameState gameState = new GameState();
            bool working = true;

            try
            {
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                listener = new TcpListener(localAddr, port);
                listener.Start();

                while (working)
                {
                    if (!gameState.isReady())
                    {
                        Console.WriteLine("Aguardando conexoes");

                        // aguarda nova conexao
                        TcpClient client = listener.AcceptTcpClient();
                        gameState.addPlayer(client);
                    }
                    else
                    {
                        // o jogo comecou
                        gameState.play();
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("Erro de rede: {0}", e);
            }
            finally
            {
                listener.Stop();
            }
        }