using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDP_prijem
{
    class Program
    {
        static void Main(string[] args)
        {
            IPEndPoint odesilatel = new IPEndPoint(IPAddress.Any, 0);
            int pom = 0;

            using (UdpClient UDP_nacluchac = new UdpClient(55000))
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("./test.txt"))
            {     
                file.WriteLine("Testovani spravnosti sekvence prichazejicich dat");

                int posun = 8;      //nastavení odkud se maji data zacit testovat

                while (!Console.KeyAvailable)
                {
                    try
                    {
                        if (UDP_nacluchac.Available == 0)
                            continue;

                        byte[] prijmute_bajty = UDP_nacluchac.Receive(ref odesilatel);          

                        byte zacatek = prijmute_bajty[posun + 1];
                        byte hodnota = zacatek;

                        for (int k = posun; k < prijmute_bajty.Length - posun; k += 2)
                        {

                            if (prijmute_bajty[k] != 125) //0x7d
                            {
                                Console.WriteLine(prijmute_bajty[k] + " Cas   " + k);
                                file.WriteLine(prijmute_bajty[k] + " Cas   " + k);
                            }
                            if (prijmute_bajty[k + 1] != hodnota++)
                            {
                                Console.WriteLine(prijmute_bajty[k + 1] + "\t\t" + (hodnota - 1) + "\t\t" + k);
                                file.WriteLine(prijmute_bajty[k + 1] + "\t\t" + (hodnota - 1) + "\t\t" + k);
                            }

                            if (hodnota == 80 || prijmute_bajty[k + 1] == 79)
                                hodnota = 65; //0x41 'A'
                        }
                        Console.WriteLine("....................." + pom++);
                        file.WriteLine("....................." + pom);                
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
                file.Flush();
            }
        }
    }
}
