public static void LoadDictionary(string filePath) { #region Loading the dictionary from the file ArgumentHandlerClass path = new ArgumentHandlerClass(); string line; try { Whois.mutex.WaitOne(); // Locks to prevent mutliple threads from writing to the file using (StreamReader dictionaryReader = new StreamReader(filePath)) { Console.WriteLine("Loading dictionary"); while ((line = dictionaryReader.ReadLine()) != null) { string[] entry = line.Split('&'); // Reads the dictionary text file line by line and splits the user and it's location based on the '&' mentioned previously in saveDictionary() if (!locations.ContainsKey(entry[0])) // First checks to see if the user already exists in the library and if it doesn't then it adds the user and it's location from the dicitonary text file { locations.Add(entry[0], entry[1]); } } Console.WriteLine("Loading complete"); dictionaryReader.Close(); } Whois.mutex.ReleaseMutex(); // Unlocks to allow the next thread in } catch (Exception e) { Console.WriteLine("Error in LoadDictionary()"); Whois.exceptionHandler(e); } #endregion }
public static void runServer(string[] args) { TcpListener listener; HandlerClass requestHandler = new HandlerClass(); ArgumentHandlerClass argumentHandler = new ArgumentHandlerClass(); string logPath = "logfile.txt"; string filePath = "locations.txt"; try // Creates the server and connection { #region Connection listener = new TcpListener(IPAddress.Any, 43); listener.Start(); Console.WriteLine("Server started listening"); ThreadPool.SetMinThreads(1000, 1000); ThreadPool.SetMaxThreads(1500, 1500); while (true) // Continuously allows clients to connect to the server { connection = listener.AcceptSocket(); NetworkStream socketStream; socketStream = new NetworkStream(connection); socketStream.ReadTimeout = 1000; DictionaryClass.LoadDictionary(filePath); // Starts by loading the dictionary file and storing the contents into the server's dictionary filePath = argumentHandler.filePathHandler(args, filePath); logPath = argumentHandler.logPathHandler(args, logPath); ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state) { requestHandler.doRequest(listener, connection, socketStream, logPath); }), null); // Starts the request for mutliple threads DictionaryClass.SaveDictionary(filePath); } #endregion } catch (Exception e) { Console.WriteLine("Error in RunServer()"); exceptionHandler(e); } }