Advertisement
IvanNikolov2217

rad 4

May 19th, 2022
432
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using BusinessLayer;
  9.  
  10. namespace Client
  11. {
  12.     public static class ClientManager
  13.     {
  14.         static TcpClient client;
  15.         static Socket clientSocket;
  16.         static IPAddress clientIPAddress, serverIPAddress;
  17.         static int clientPort, serverPort;
  18.         static string clientIP, serverIP;
  19.         public static bool CommunicationIsActive = true;
  20.  
  21.         public static void InitializeClient()
  22.         {
  23.             Console.Write("Enter server IP address to use (or nothing for local IP): ");
  24.             serverIP = Console.ReadLine();
  25.             serverIP = (serverIP != string.Empty) ? serverIP : "127.0.0.1";
  26.  
  27.             Console.Write("Enter server port to use: ");
  28.             serverPort = Convert.ToInt32(Console.ReadLine());
  29.  
  30.             Console.Write("Enter client IP address to use (or nothing for local IP): ");
  31.             clientIP = Console.ReadLine();
  32.             clientIP = (clientIP != string.Empty) ? clientIP : "127.0.0.1";
  33.  
  34.             Console.Write("Enter client port to use: ");
  35.             clientPort = Convert.ToInt32(Console.ReadLine());
  36.  
  37.             serverIPAddress = IPAddress.Parse(serverIP);
  38.             clientIPAddress = IPAddress.Parse(clientIP);
  39.  
  40.             IPEndPoint endPoint = new IPEndPoint(clientIPAddress, clientPort);
  41.             client = new TcpClient(endPoint);
  42.  
  43.             clientSocket = client.Client;
  44.  
  45.             client.Connect(serverIPAddress, serverPort);
  46.  
  47.             Console.WriteLine("Socket local endpoint: {0}", clientSocket.LocalEndPoint.ToString());
  48.             Console.WriteLine("Socket connected: {0}", clientSocket.Connected);
  49.         }
  50.  
  51.         public static void SendMessage(BinaryMessage binaryMessage)
  52.         {
  53.             clientSocket.Send(binaryMessage.Data);
  54.  
  55.             Console.WriteLine("Client sent {0} bytes to the server!", binaryMessage.Data.Length);
  56.         }
  57.  
  58.         public static Dictionary<Type, object> WaitForMessage()
  59.         {
  60.             BinaryMessage binaryMessage = new BinaryMessage();
  61.  
  62.             clientSocket.Receive(binaryMessage.Data);
  63.  
  64.             return RecieveMessage(binaryMessage);
  65.         }
  66.  
  67.         private static Dictionary<Type, object> RecieveMessage(BinaryMessage binaryMessage)
  68.         {
  69.             object obj = TransformDataManager.Deserialize(binaryMessage);
  70.  
  71.             if (obj is string)
  72.             {
  73.                 return new Dictionary<Type, object>()
  74.                 {
  75.                     { typeof(string), obj.ToString() }
  76.                 };
  77.             }
  78.             else if (obj is int)
  79.             {
  80.                 return new Dictionary<Type, object>()
  81.                 {
  82.                     { typeof(int), int.Parse(obj.ToString()) }
  83.                 };
  84.             }
  85.             else if (obj is Student)
  86.             {
  87.                Student student = obj as Student;
  88.  
  89.                 return new Dictionary<Type, object>()
  90.                 {
  91.                     { typeof(Student), student }
  92.                 };
  93.             }
  94.             else if (obj is Classroom)
  95.             {
  96.                 Classroom classroom = obj as Classroom;
  97.  
  98.                 return new Dictionary<Type, object>()
  99.                 {
  100.                     { typeof(Classroom), classroom }
  101.                 };
  102.             }
  103.             else if (obj is List<Student>)
  104.             {
  105.                 List<Student> students = obj as List<Student>;
  106.  
  107.                 return new Dictionary<Type, object>()
  108.                 {
  109.                     { typeof(List<Student>), students }
  110.                 };
  111.             }
  112.             else if (obj is List<Classroom>)
  113.             {
  114.                 List<Classroom> classrooms = obj as List<Classroom>;
  115.  
  116.                 return new Dictionary<Type, object>()
  117.                 {
  118.                     { typeof(List<Classroom>), classrooms }
  119.                 };
  120.             }
  121.             else
  122.             {
  123.                 throw new ArgumentException("Unsupported type format!");
  124.             }
  125.         }
  126.  
  127.         public static bool ContinueCommunicating()
  128.         {
  129.             Console.WriteLine("Continue sending data to the server? [y/Y or n/N]");
  130.             string input = Console.ReadLine();
  131.  
  132.             if (input.ToLower() == "y")
  133.             {
  134.                 return true;
  135.             }
  136.             else
  137.             {
  138.                 return false;
  139.             }
  140.         }
  141.  
  142.         public static void CloseConnection()
  143.         {
  144.             client.Close();
  145.         }
  146.  
  147.     }
  148. }
  149.  
Advertisement
RAW Paste Data Copied
Advertisement