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(); } }
private static string SetPassword() //sets the login password that each client must input in order to login { Console.WriteLine("Choose a password, must be at least 4 chars length, and only alpahnumerical"); Console.Write("Enter the password: "******" "; while (!Regex.IsMatch(password, "^[a-zA-Z0-9]*$")) //the password must be alphanumerical { Console.Write("Enter the password: "); password = Console.ReadLine(); } PreCon.SendApproval(); //once a password is set, all clients are informed Thread t = new Thread(new ThreadStart(PreCon.AfterStart)); t.Start(); return(password); }