public static BitArray decoddingByteToBitArray(byte[] byteArray) { BitArray bitArray = new BitArray(byteArray); byte[] tempByte = CoddingClass.CoddingBitToByteArray(bitArray); bitArray = new BitArray(tempByte); return(bitArray); }
public static void StartListener(ref BitArray bitArraySession11) { UdpClient listener = new UdpClient(listenPort); IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort); int negative_number_counter = 0; try { poczatek: byte[] bytes = listener.Receive(ref groupEP); BitArray recvBitArray = DecoddingClass.decoddingByteToBitArray(bytes); if (recvBitArray[2] == false && recvBitArray[3] == false && (recvBitArray[4] == false || recvBitArray[4] == true)) { CoddingClass.CoddingBitToIntArray(ref result, recvBitArray); Console.WriteLine("Wynik: " + result[0]); } else if (recvBitArray[2] == false && recvBitArray[3] == true && recvBitArray[4] == false) { CoddingClass.CoddingBitToIntArray(ref result, recvBitArray); negative_number_counter++; if (negative_number_counter > 0) { Console.WriteLine("Wynik: -" + result[0]); } else { Console.WriteLine(result[0]); } } else if (recvBitArray[2] == true && recvBitArray[3] == true && recvBitArray[4] == false && recvBitArray.Count == 80) {//Błąd histori Console.WriteLine("Historia jest pusta!"); } else if (recvBitArray[2] == true && recvBitArray[3] == false && recvBitArray[4] == true && recvBitArray.Count == 112) //Odbiór historii z użyciem sesji { //ilość plików historii CoddingClass.CoddingBitToIntArray(ref result, recvBitArray); check = result[0]; goto poczatek; } else if (recvBitArray[2] == false && recvBitArray[3] == true && recvBitArray[4] == true && recvBitArray.Count == 176) {//odbieranie historii historyInt = CoddingClass.CoddingBitToIntArrayHistory(recvBitArray); if (recvBitArray[0] == false && recvBitArray[1] == false) { if (historyInt[0] * historyInt[1] != historyInt[2]) { Console.WriteLine(historyInt[0] + " ! " + " = " + historyInt[2]); } else { Console.WriteLine(historyInt[0] + " * " + historyInt[1] + " = " + historyInt[2]); } } else if (recvBitArray[0] == false && recvBitArray[1] == true) { Console.WriteLine(historyInt[0] + " : " + historyInt[1] + " = " + historyInt[2]); } else if (recvBitArray[0] == true && recvBitArray[1] == false) { Console.WriteLine(historyInt[0] + " + " + historyInt[1] + " = " + historyInt[2]); } else if (recvBitArray[0] == true && recvBitArray[1] == true) { Console.WriteLine(historyInt[0] + " - " + historyInt[1] + " = " + historyInt[2]); } check--; if (check > 0) { goto poczatek; } } else if (recvBitArray[2] == true && recvBitArray[3] == false && recvBitArray[4] == true && recvBitArray.Count == 176) {//odbieranie historii historyInt = CoddingClass.CoddingBitToIntArrayHistory(recvBitArray); if (recvBitArray[0] == false && recvBitArray[1] == false) { if (historyInt[0] * historyInt[1] != historyInt[2]) { Console.WriteLine(historyInt[0] + " ! " + " = " + historyInt[2]); } else { Console.WriteLine(historyInt[0] + " * " + historyInt[1] + " = " + historyInt[2]); } } else if (recvBitArray[0] == false && recvBitArray[1] == true) { Console.WriteLine(historyInt[0] + " : " + historyInt[1] + " = " + historyInt[2]); } else if (recvBitArray[0] == true && recvBitArray[1] == false) { Console.WriteLine(historyInt[0] + " + " + historyInt[1] + " = " + historyInt[2]); } else if (recvBitArray[0] == true && recvBitArray[1] == true) { Console.WriteLine(historyInt[0] + " - " + historyInt[1] + " = " + historyInt[2]); } check--; if (check > 0) { goto poczatek; } } else if (recvBitArray[2] == true && recvBitArray[3] == false && recvBitArray[4] == false && recvBitArray.Count == 112) //Odbiór historii z użyciem sesji { //ilość plików historii CoddingClass.CoddingBitToIntArray(ref result, recvBitArray); check = result[0]; goto poczatek; } else if (recvBitArray[2] == true && recvBitArray[3] == false && recvBitArray[4] == false && recvBitArray.Count == 176) {//odbieranie historii historyInt = CoddingClass.CoddingBitToIntArrayHistory(recvBitArray); if (recvBitArray[0] == false && recvBitArray[1] == false) { if (historyInt[0] * historyInt[1] != historyInt[2]) { Console.WriteLine(historyInt[0] + " ! " + " = " + historyInt[2]); } else { Console.WriteLine(historyInt[0] + " * " + historyInt[1] + " = " + historyInt[2]); } } //Console.WriteLine(historyInt[0] + " * " + historyInt[1] + " = " + historyInt[2]); else if (recvBitArray[0] == false && recvBitArray [1] == true) { Console.WriteLine(historyInt[0] + " : " + historyInt[1] + " = " + historyInt[2]); } else if (recvBitArray[0] == true && recvBitArray[1] == false) { Console.WriteLine(historyInt[0] + " + " + historyInt[1] + " = " + historyInt[2]); } else if (recvBitArray[0] == true && recvBitArray[1] == true) { Console.WriteLine(historyInt[0] + " - " + historyInt[1] + " = " + historyInt[2]); } check--; if (check > 0) { goto poczatek; } } else if (recvBitArray[0] == false && recvBitArray[1] == true && recvBitArray[2] == true && recvBitArray[3] == true && recvBitArray[4] == true && recvBitArray.Count == 80) { Console.WriteLine("Błąd dzielenia przez zero!"); } else if (recvBitArray[2] == true && recvBitArray[3] == true && recvBitArray[4] == true) { Console.WriteLine("Wynik znajduje się poza zakresem <0,{0}>", int.MaxValue); } CoddingClass.CoddingSession(ref bitArraySession11, recvBitArray); } catch (SocketException e) { Console.WriteLine(e); } finally { listener.Close(); } }
public static void factorial(string action, ref byte[] dataSend, ref BitArray sessionBitArray) { BitArray bitArrayToCode = new BitArray(bitSendLenght, false); int checkSession = 0; double pow = 10; char nowy; poczatek: int checkValue1 = 0; Console.Write("Podaj liczbe: "); val_1 = Console.ReadLine(); for (int i = 0; i < val_1.Length; i++) { nowy = val_1[i]; if (Char.IsDigit(nowy) == false && nowy != '-') { checkValue1++; } } if (checkSession == 0) { for (int i = 0; i < sessionBitArray.Count; i++) //Sprawdzanie czy sesja została już nadana { if (sessionBitArray[i] == true) { checkSession += (int)Math.Pow(2, pow); } pow--; } } if (checkSession == 0) //Nadanie sesji { for (int i = 0, j = 133; i < sessionBitArray.Count && j < bitArrayToCode.Count; i++, j++) { bitArrayToCode[j] = sessionBitArray[i]; } } if (checkValue1 > 0) { Console.WriteLine("Proszę wprowadzić same cyfry!"); goto poczatek; } else if (val_1 == "" || long.Parse(val_1) < 0 || long.Parse(val_1) > int.MaxValue) { Console.WriteLine("Liczba z poza przedziału! Dostępny przedział to <0,{0}>", int.MaxValue); goto poczatek; } else if (int.Parse(val_1) > 12) { Console.WriteLine("Wynik moze znadjdowac się poza zakresem zmiennej, podaj liczbe mniejsza niż 12!"); goto poczatek; } else { //Kodowanie i odwracanie val_1 byte_1[0] = int.Parse(val_1); BitArray bit_1 = new BitArray(byte_1); bit_1 = ReverseClass.reverseOrder(bit_1); //Kodowanie i odwracanie val_2 byte_2[0] = 0; BitArray bit_2 = new BitArray(byte_2); CoddingClass.CoddingBitArray(action, bit_1, bit_2, ref bitArrayToCode, sessionBitArray); dataSend = CoddingClass.CoddingBitToByteArray(bitArrayToCode); } }
public static void Calculation(string action, ref byte[] dataSend, ref BitArray sessionBitArray) { int checkSession = 0; double pow = 10; BitArray bitArrayToCode = new BitArray(bitSendLenght, false); //MAIN BIT -144 elementowa tablica bitów poczatek: Console.Write("Podaj liczbe 1 i 2: "); val_1 = Console.ReadLine(); val_2 = Console.ReadLine(); char nowy; int checkValue1 = 0; int checkValue2 = 0; for (int i = 0; i < val_1.Length; i++) { nowy = val_1[i]; if (Char.IsDigit(nowy) == false && nowy != '-') { checkValue1++; } } for (int i = 0; i < val_2.Length; i++) { nowy = val_2[i]; if (Char.IsDigit(nowy) == false && nowy != '-') { checkValue2++; } } if (checkSession == 0) { for (int i = 0; i < sessionBitArray.Count; i++) //Sprawdzanie czy sesja została już nadana { if (sessionBitArray[i] == true) { checkSession += (int)Math.Pow(2, pow); } pow--; } } if (checkSession == 0) //Nadanie sesji { for (int i = 0, j = 133; i < sessionBitArray.Count && j < bitArrayToCode.Count; i++, j++) { bitArrayToCode[j] = sessionBitArray[i]; } } if (checkValue1 > 0 || checkValue2 > 0) { Console.WriteLine("Proszę wprowadzić same cyfry!"); goto poczatek; } else if (val_1 == "" || val_2 == "" || long.Parse(val_1) < 0 || long.Parse(val_1) > int.MaxValue || long.Parse(val_2) < 0 || long.Parse(val_2) > int.MaxValue) { Console.WriteLine("Liczba z poza przedziału! Dostępny przedział to <0,{0}>", int.MaxValue); goto poczatek; } //else if (action == "dz" && int.Parse(val_2) == 0) //{ // Console.WriteLine("Błąd dzielenia przez 0!"); // goto poczatek; //} else if (action == "dz" && int.Parse(val_1) < int.Parse(val_2)) { Console.WriteLine("System operuje na liczbach całkowitych, zatem proszę podac dzielnik mniejszy od licznika!"); goto poczatek; } else if (action == "o" && int.Parse(val_1) < int.Parse(val_2)) { Console.WriteLine("System operuje na liczbach dodatnich, zatem wartość druga nie może być większa od wartości pierwszej!"); goto poczatek; } else if (checkValue1 > 0 || checkValue2 > 0) { Console.WriteLine("Proszę wprowadzić same cyfry!"); goto poczatek; } else //operacje obliczania { //Kodowanie i odwracanie val_1 byte_1[0] = int.Parse(val_1); BitArray bit_1 = new BitArray(byte_1); bit_1 = ReverseClass.reverseOrder(bit_1); //Kodowanie i odwracanie val_2 byte_2[0] = int.Parse(val_2); BitArray bit_2 = new BitArray(byte_2); bit_2 = ReverseClass.reverseOrder(bit_2); //Kodujemy ciąg 144 bitów CoddingClass.CoddingBitArray(action, bit_1, bit_2, ref bitArrayToCode, sessionBitArray); //WriteClass.WriteBitTab(bitArrayToCode); dataSend = CoddingClass.CoddingBitToByteArray(bitArrayToCode); } }
static void Main(string[] args) { int[] sesja = new int[1]; int licznik = 0; int bitSendLenght = 144; //ilość wysyłanych bitów string IPserver = "127.0.0.1"; //IP odbiorcy - servera int port = 8080; //Port IPEndPoint sendingEndPoint = new IPEndPoint(IPAddress.Parse(IPserver), port); //Miejsce gdzie będzie wysyłane Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //Określenie protokołów z których korzysta klient string action; //Pole okreslające akcje użytkownika byte[] dataSend = new byte[bitSendLenght / 8]; //tablica która będziemy wysyłać dane byte[] historyRequest80 = new byte[10]; //tablica wykorzystywana do wysłania żądania odnośnie historii byte[] historyRequest112 = new byte[14]; //tablica wykorzystywana do wysłania żądania odnośnie historii Console.WriteLine("Witaj podaj, co chcesz zrobić:\n d -dodawanie\n o -odejmowanie\n m -mnożenie\n dz -dzielenie\n s -silnia\n hs -historia z użyciem sejsi\n hi -historia z użyciem ID obliczen\n hsp -historia z podanym ID sesji\n hip -historia z podanym ID obliczeń\n cls -wyczyść konsole\n"); while (true) { if (licznik == 1) { CoddingClass.SessionToIntArray(ref sesja, bitArraySession11); Console.WriteLine("\nUzyskano sesje: " + sesja[0]); } action = Console.ReadLine(); if (action == "cls") { licznik++; Console.Clear(); Console.WriteLine("Numer sesji: " + sesja[0]); Console.WriteLine("Witaj podaj, co chcesz zrobić:\n d -dodawanie\n o -odejmowanie\n m -mnożenie\n dz -dzielenie\n s -silnia\n hs -historia z użyciem sejsi\n hi -historia z użyciem ID obliczen\n hsp -historia z podanym ID sesji\n hip -historia z podanym ID obliczeń\n cls -wyczyść konsole\n"); continue; } else if (action == "d" || action == "m" || action == "o" || action == "dz" /*|| action == "s" || action == "h"*/) { CalculationClass.Calculation(action, ref dataSend, ref bitArraySession11); } else if (action == "s") { CalculationClass.factorial(action, ref dataSend, ref bitArraySession11); } else if (action == "hs" || action == "hi" || action == "hip" || action == "hsp") { if (action == "hs" || action == "hi") { historyRequest80 = HistorClass.HistoryRequest(action, bitArraySession11); } else { historyRequest112 = HistorClass.HistoryRequest(action, bitArraySession11); } } else { Console.WriteLine("Podana operacja nie istnieje!"); continue; } if (action == "hs" || action == "hi") { client.SendTo(historyRequest80, sendingEndPoint); } else if (action == "hip" || action == "hsp") { client.SendTo(historyRequest112, sendingEndPoint); } else { client.SendTo(dataSend, sendingEndPoint); } ListenClass.StartListener(ref bitArraySession11); licznik++; } Console.ReadLine(); }