Beispiel #1
0
        // Servers most important running method
        private void RunService()
        {
            // Important variable instantiation
            players = new List<Player>();
            cookies = new List<Cookie>();
            gameState = new Data(64, 64);

            //Create new thread for updating cookies
            new Timer(CookieTick, null, TimeSpan.Zero, TimeSpan.FromSeconds(0.3));

            // Windows or Unix localhost
            string localHost = Environment.OSVersion.Platform == PlatformID.Unix ? "0.0.0.0" : "127.0.0.1";

            // Setup the TCP socket
            _listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _listeningSocket.Bind(new IPEndPoint(IPAddress.Parse(localHost), _port)); // Always localhost
            _listeningSocket.Listen(_port);

            //Debug info
            Console.WriteLine("Listening for connections on port " + _port + " and localhost IP: " + localHost);

            // Socket listening loop
            while (true)
            {
                Player player = new Player(_listeningSocket.Accept());
                players.Add(player);
                Thread thread = new Thread(() => player.Run());
                thread.Start();
            }
        }