6237 Programmering II (Csharp) Agenda/solution1

From Teknologisk videncenter
< 6237 Programmering II (Csharp) Agenda
Revision as of 08:35, 16 February 2016 by Micn (talk | contribs) (Created page with "<source lang=csharp> class Program { static StreamWriter outFile; static StreamReader inFile; static List<string> arpPackets = new List<string>(); ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
class Program
    {
        static StreamWriter outFile;
        static StreamReader inFile;

        static List<string> arpPackets = new List<string>();
        static List<string> dnsPackets = new List<string>();

        //     47 4.635842000    172.16.236.177        8.8.8.8               DNS      85     Standard query 0x2063  A lh4.googleusercontent.com
        static void analyze(List<String> lines)
        {
            if (lines.Count < 2) return;

            //Protokol navnet står fra plads 69 og 9 pladser frem
            string protocol = lines[1].Substring(67, 9);

            //Tilføj også kode der kan læse Source og Destination
            string source = lines[1].Substring(23, 22).Trim();
            string dest = lines[1].Substring(45, 22).Trim();

            string info = lines[1].Substring(83).Trim();

            if (protocol.Trim().ToUpper() == "ARP")
            {
                arpPackets.Add(source);
            }

            if (protocol.Trim().ToUpper() == "DNS")
            {
                //Tilføj kode her der tæller antallet af DNS og gemmer den URL der søges på
                if (info.Contains("Standard query 0x")) {
                    string[] infoSplit = info.Split(' ');
                    dnsPackets.Add(infoSplit[5]);
                }

            }
        }

        static Dictionary<string, int> web = new Dictionary<string, int>();
        static void AddWebAddress(String address)
        {
            if (web.ContainsKey(address))
                web[address]++;
            else
                web[address] = 1;
        }


        //static void Main(string[] args)
        static void Main(string[] args)
        {
            inFile = new StreamReader("logfile.txt");
            outFile = new StreamWriter("outLogs.txt");

            String line;
            List<String> lines = new List<string>();

            while (!inFile.EndOfStream)
            {
                line = inFile.ReadLine();
                //Hvis line starter med No. er det en ny pakke, så skal vi først analysere den gamle
                if (line.StartsWith("No.") || inFile.EndOfStream)
                {
                    analyze(lines);
                    lines = new List<string>();
                }
                lines.Add(line);
            }
            inFile.Close();
            outFile.Close();

            foreach (string url in dnsPackets)
            {
                AddWebAddress(url);
            }

            foreach (string key in web.Keys)
            {
                System.Console.WriteLine("Adressen {0} optræder {1} gange", key, web[key]);
            }

            Console.ReadLine();
        }
    }