aboutsummaryrefslogtreecommitdiff
blob: 4ae77a33e916dcb7c31110a60d6b67e9527b596f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace PasswordCrackerServer
{
    class Program
    {
        static int NumberOfClients = 1;

        static void Main(string[] args)
        {
            List<UserInfo> userInfos = PasswordFileHandler.ReadPasswordFile("passwords.txt");
            List<clientConnection> clients = new List<clientConnection>();
            List<string> allChunks =  new List<string>();

            string users = "";
            foreach (UserInfo u in userInfos)
                users += u + "|";

            var dictionary = File.ReadAllText("webster-dictionary.txt");
            dictionary = dictionary.Replace("\r\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<string[]> RunAsync(List<clientConnection> clients)
        {
            List<Task<string>> tasks = new List<Task<string>>();
            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;
        }

    }
}