From f68bb1864b40c69d12b44db5aa33125aef39e150 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Sat, 17 Mar 2018 15:33:00 +0100 Subject: Restructuring --- DistributedPasswordCracker.Server/Program.cs | 108 +++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 DistributedPasswordCracker.Server/Program.cs (limited to 'DistributedPasswordCracker.Server/Program.cs') diff --git a/DistributedPasswordCracker.Server/Program.cs b/DistributedPasswordCracker.Server/Program.cs new file mode 100644 index 0000000..db8940a --- /dev/null +++ b/DistributedPasswordCracker.Server/Program.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using DistributedPasswordCracker.Server.Models; +using DistributedPasswordCracker.Server.Utilities; + +namespace DistributedPasswordCracker.Server +{ + class Program + { + static int NumberOfClients = 1; + + static void Main(string[] args) + { + List userInfos = PasswordFileHandler.ReadPasswordFile("passwords.txt"); + List clients = new List(); + List allChunks = new List(); + + string users = ""; + foreach (UserInfo u in userInfos) + users += u + "|"; + + var dictionary = File.ReadAllText("dictionary.txt"); + dictionary = dictionary.Replace('\n', '|'); + var splitDictionary = SplitDictionary(dictionary); + for (int i = 0; i < splitDictionary.GetLength(0); i++) + { + StringBuilder chunk = new StringBuilder(); + for (int j = 0; j < splitDictionary.GetLength(1); j++) + { + chunk.Append(splitDictionary[i, j]); + chunk.Append('|'); + } + allChunks.Add(chunk.ToString()); + } + + IPAddress ip = IPAddress.Any; + TcpListener serversocket = new TcpListener(ip, 6789); + + serversocket.Start(); + Console.WriteLine("Server started."); + int counter = 0; + while (true) + { + if (clients.Count == NumberOfClients) + { + string[] returnedResult = RunAsync(clients).Result; + foreach (string s in returnedResult) + Console.WriteLine(s); + } + else + { + TcpClient connectionSocket = serversocket.AcceptTcpClient(); + Console.WriteLine("Client connected."); + + clients.Add(new ClientConnection(connectionSocket, allChunks[counter], users)); + counter++; + } + } + serversocket.Stop(); + } + + private static string[,] SplitDictionary(string dictionary) + { + string[] dicWords = dictionary.Split('|'); + int dicWordsLength = dicWords.Length; + string[,] splitDictionary = new string[NumberOfClients, dicWordsLength / NumberOfClients]; + int offset = 0; + int j = 0; + + for (int i = 0; i < NumberOfClients; i++) + { + while (j < dicWordsLength / NumberOfClients) + { + splitDictionary[i, j] = dicWords[j + offset]; + j++; + } + offset += dicWordsLength / NumberOfClients; + j = 0; + } + + return splitDictionary; + } + + public static async Task RunAsync(List clients) + { + List> tasks = new List>(); + for (int i = 0; i < NumberOfClients; i++) + tasks[i] = clients[i].SendToClient(); + + string[] result = await Task.WhenAll(tasks); + + /* + var C1 = await C1task; + var C2 = await C2task; + var C3 = await C3task; + var C4 = await C4task; + var C5 = await C5task; + */ + return result; + } + + } +} \ No newline at end of file -- cgit v1.2.3