示例#1
0
        static void Main(string[] args)
        {
            PreCon.FreeTcpPort();                       //finds a free port
            PreCon.PreSock();                           //tries to connect to each address
            Server.SetCommands();                       //sets the server's commands
            WmiFuncs.AddPaths(Environment.MachineName); //saves the server's local shared folders
            byte[]      bytes      = new byte[4096];
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());

            for (int i = 0; i < ipHostInfo.AddressList.Length; i++)
            {
                if (ipHostInfo.AddressList[i].ToString().StartsWith("192"))
                {
                    ipAddress = ipHostInfo.AddressList[i];
                }
            }

            // builds the socket and starts listening for clients
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
            Socket     listener      = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listener.Bind(localEndPoint);
            string pass = SetPassword();

            Console.WriteLine("listening on ip: " + ipAddress + " port: " + port);
            listener.Listen(3);
            while (true)
            {
                Socket handler = listener.Accept();
                Console.WriteLine("A client with the ip of " + handler.RemoteEndPoint + " has connected");
                //once a new client is connected, starts the socket maintenance function for him on a separate thread
                Thread t = new Thread(() => KeepInTact(handler, pass));
                t.Start();
            }
        }