From e866abc9e35e62842168d01199e90f2bdfaf774e Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Sat, 17 Mar 2018 17:54:36 +0100 Subject: Fixed everything --- DistributedPasswordCracker.Server/Program.cs | 63 ++++++++++++---------------- 1 file changed, 27 insertions(+), 36 deletions(-) (limited to 'DistributedPasswordCracker.Server/Program.cs') diff --git a/DistributedPasswordCracker.Server/Program.cs b/DistributedPasswordCracker.Server/Program.cs index db8940a..ef8f93f 100644 --- a/DistributedPasswordCracker.Server/Program.cs +++ b/DistributedPasswordCracker.Server/Program.cs @@ -12,12 +12,11 @@ namespace DistributedPasswordCracker.Server { class Program { - static int NumberOfClients = 1; + static int NumberOfClients = 3; static void Main(string[] args) { List userInfos = PasswordFileHandler.ReadPasswordFile("passwords.txt"); - List clients = new List(); List allChunks = new List(); string users = ""; @@ -38,30 +37,24 @@ namespace DistributedPasswordCracker.Server allChunks.Add(chunk.ToString()); } - IPAddress ip = IPAddress.Any; - TcpListener serversocket = new TcpListener(ip, 6789); - - serversocket.Start(); + TcpListener serverSocket = new TcpListener(IPAddress.Any, 7777); + 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."); + + List> tasks = new List>(); - clients.Add(new ClientConnection(connectionSocket, allChunks[counter], users)); - counter++; - } + for (int i = 0; i < NumberOfClients; i++) + { + var chunk = allChunks[i]; + tasks.Add(Task.Run(()=>RunClientAsync(serverSocket, users, chunk))); } - serversocket.Stop(); + string[] result = Task.WhenAll(tasks).Result; + + foreach (string s in result) + Console.WriteLine(s); + + serverSocket.Stop(); } private static string[,] SplitDictionary(string dictionary) @@ -86,23 +79,21 @@ namespace DistributedPasswordCracker.Server return splitDictionary; } - public static async Task RunAsync(List clients) + private async static Task RunClientAsync(TcpListener serverSocket, string pass, string chunk) { - List> tasks = new List>(); - for (int i = 0; i < NumberOfClients; i++) - tasks[i] = clients[i].SendToClient(); + TcpClient connectionSocket = serverSocket.AcceptTcpClient();; + Stream ns = connectionSocket.GetStream(); + StreamReader sr = new StreamReader(ns); + StreamWriter sw = new StreamWriter(ns) + { + AutoFlush = true + }; - string[] result = await Task.WhenAll(tasks); + sw.WriteLine($"DPCP 1.0\n{pass}\n{chunk}\n\n"); + + var result = await sr.ReadLineAsync(); - /* - 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