Advertisement
IvanNikolov2217

rad 7

May 19th, 2022
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.57 KB | None | 0 0
  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 Server
  11. {
  12.     public static class ServerManager
  13.     {
  14.         static TcpListener server;
  15.         static Socket serverSocket, clientSocket;
  16.         static IPAddress serverIPAddress;
  17.         static int maxConnections, serverPort;
  18.         static string serverIP;
  19.         public static bool CommunicationIsActive = true;
  20.  
  21.         public static void InitializeServer()
  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 maximum clients: ");
  31.             maxConnections = Convert.ToInt32(Console.ReadLine());
  32.  
  33.             serverIPAddress = IPAddress.Parse(serverIP);
  34.             server = new TcpListener(serverIPAddress, serverPort);
  35.  
  36.             serverSocket = server.Server;
  37.  
  38.             server.Start(maxConnections);
  39.  
  40.             Console.WriteLine("Socket local endpoint: {0}", serverSocket.LocalEndPoint.ToString());
  41.         }
  42.  
  43.         public static void ListenForNewConnections()
  44.         {
  45.             clientSocket = server.AcceptSocket();
  46.  
  47.             Console.WriteLine("Socket connected: {0}", clientSocket.Connected);
  48.             Console.WriteLine("Socket remote endpoint: {0}", clientSocket.RemoteEndPoint.ToString());
  49.         }
  50.  
  51.         public static Dictionary<Type, object> WaitForMessage()
  52.         {
  53.             BinaryMessage binaryMessage = new BinaryMessage();
  54.  
  55.             clientSocket.Receive(binaryMessage.Data);
  56.  
  57.             return RecieveMessage(binaryMessage);
  58.         }
  59.  
  60.         private static Dictionary<Type, object> RecieveMessage(BinaryMessage binaryMessage)
  61.         {
  62.             object obj = TransformDataManager.Deserialize(binaryMessage);
  63.  
  64.             if (obj is string)
  65.             {
  66.                 return new Dictionary<Type, object>()
  67.                 {
  68.                     { typeof(string), obj.ToString() }
  69.                 };
  70.             }
  71.             else if (obj is int)
  72.             {
  73.                 return new Dictionary<Type, object>()
  74.                 {
  75.                     { typeof(int), int.Parse(obj.ToString()) }
  76.                 };
  77.             }
  78.             else if (obj is Student)
  79.             {
  80.                 Student student = obj as Student;
  81.  
  82.                 return new Dictionary<Type, object>()
  83.                 {
  84.                     { typeof(Student), student }
  85.                 };
  86.             }
  87.             else if (obj is Classroom)
  88.             {
  89.                 Classroom classroom = obj as Classroom;
  90.  
  91.                 return new Dictionary<Type, object>()
  92.                 {
  93.                     { typeof(Classroom), classroom }
  94.                 };
  95.             }
  96.             else if (obj is List<Student>)
  97.             {
  98.                 List<Student> students = obj as List<Student>;
  99.  
  100.                 return new Dictionary<Type, object>()
  101.                 {
  102.                     { typeof(List<Student>), students }
  103.                 };
  104.             }
  105.             else if (obj is List<Classroom>)
  106.             {
  107.                 List<Classroom> classrooms = obj as List<Classroom>;
  108.  
  109.                 return new Dictionary<Type, object>()
  110.                 {
  111.                     { typeof(List<Classroom>), classrooms }
  112.                 };
  113.             }
  114.             else
  115.             {
  116.                 throw new ArgumentException("Unsupported type format!");
  117.             }
  118.         }
  119.  
  120.         public static void SendMessage(BinaryMessage binaryMessage)
  121.         {
  122.             clientSocket.Send(binaryMessage.Data);
  123.  
  124.             Console.WriteLine("Server sent {0} bytes to the client!", binaryMessage.Data.Length);
  125.         }
  126.  
  127.         public static bool ContinueListening()
  128.         {
  129.             Console.WriteLine("Continue listening for new connections? [y/Y or n/N]");
  130.             string input = Console.ReadLine();
  131.  
  132.             if (input.ToLower() == "y")
  133.             {
  134.                 CommunicationIsActive = true;
  135.                 return true;
  136.             }
  137.             else
  138.             {
  139.                 CommunicationIsActive = false;
  140.                 return false;
  141.             }
  142.         }
  143.  
  144.         public static void CloseConnection()
  145.         {
  146.             server.Stop();
  147.         }
  148.  
  149.     }
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement