Advertisement
Guest User

Untitled

a guest
May 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.59 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6.  
  7. namespace CF.NetOp
  8. {
  9.     /// <summary>
  10.     /// A Server.
  11.     /// </summary>
  12.     public class Server
  13.     {
  14.         public class RecEventArgs : EventArgs
  15.         {
  16.             public string RecS;
  17.             public byte[] RecB;
  18.             public int ListenerID;
  19.             public RecEventArgs(string received, byte[] receivedb, int LID)
  20.             {
  21.                 this.RecS = received;
  22.                 this.RecB = receivedb;
  23.                 this.ListenerID = LID;
  24.             }
  25.         }
  26.         private class Listener
  27.         {
  28.             private TcpClient tcpc;
  29.             private int ListenerID = 0;
  30.             private byte[] rbuf = new byte[512];
  31.             private bool Alive = false;
  32.             private Thread ListenT;
  33.  
  34.             /// <summary>
  35.             /// This event is being raised if we received something.
  36.             /// </summary>
  37.             public event EventHandler<RecEventArgs> Rec;
  38.  
  39.             /// <summary>
  40.             /// Creates the listener.
  41.             /// </summary>
  42.             /// <param name="AcceptedClient">The TcpListener.AcceptTcpClient() client.</param>
  43.             public Listener(ref TcpClient AcceptedClient)
  44.             {
  45.                 this.tcpc = AcceptedClient;
  46.                 this.Alive = true;
  47.                 StartListening();
  48.             }
  49.  
  50.             /// <summary>
  51.             /// Creates the listener.
  52.             /// </summary>
  53.             /// <param name="AcceptedClient">The TcpListener.AcceptTcpClient() client.</param>
  54.             /// <param name="ListenerID">An ID to identify the listener's slot.</param>
  55.             public Listener(ref TcpClient AcceptedClient, int ListenerID)
  56.             {
  57.                 this.ListenerID = ListenerID;
  58.                 this.tcpc = AcceptedClient;
  59.                 this.Alive = true;
  60.                 StartListening();
  61.             }
  62.  
  63.             private void StartListening()
  64.             {
  65.                 this.ListenT = new Thread(new ThreadStart(Listen));
  66.                 this.ListenT.Start();
  67.             }
  68.  
  69.             private void Listen()
  70.             {
  71.                 this.tcpc.GetStream().BeginRead(this.rbuf, 0, 512, Received, null);
  72.             }
  73.  
  74.             private void Received(IAsyncResult ar)
  75.             {
  76.                 int BytesRead = this.tcpc.GetStream().EndRead(ar);
  77.                 if (BytesRead < 1)
  78.                 {
  79.                     this.Alive = false;
  80.                     return;
  81.                 }
  82.  
  83.                 byte[] data = new byte[BytesRead];
  84.                 for (int i = 0; i < BytesRead; i++)
  85.                     data[i] = this.rbuf[i];
  86.                 this.tcpc.GetStream().Flush();
  87.  
  88.                 if (this.Alive)
  89.                     Listen();
  90.  
  91.                 Rec(this, new RecEventArgs(Encoding.ASCII.GetString(data), data, this.ListenerID));
  92.             }
  93.  
  94.             /// <summary>
  95.             /// Sends a preset buffer to the client.
  96.             /// </summary>
  97.             /// <param name="Data">The buffer to send.</param>
  98.             public void Send(byte[] Data)
  99.             {
  100.                 this.tcpc.GetStream().Write(Data, 0, Data.Length);
  101.                 // this.tcpc.GetStream().Flush();
  102.             }
  103.  
  104.             /// <summary>
  105.             /// Determines whether this client is alive or not.
  106.             /// </summary>
  107.             /// <returns>Returns true in case of aliveness otherwise false.</returns>
  108.             public bool IsAlive()
  109.             {
  110.                 return this.Alive;
  111.             }
  112.  
  113.             /// <summary>
  114.             /// Shuts this client down.
  115.             /// </summary>
  116.             public void Close()
  117.             {
  118.                 this.Alive = false;
  119.                 this.tcpc.Close();
  120.             }
  121.         }
  122.  
  123.         private int Port = 0;
  124.         private TcpListener tcpl;
  125.         private int ClientsCount = 0;
  126.  
  127.         private Thread CAT;
  128.         private Listener[] Listeners;
  129.         public event EventHandler<RecEventArgs> Rec;
  130.  
  131.         /// <summary>
  132.         /// Sets up a new listening port.
  133.         /// </summary>
  134.         /// <param name="Port">Port to listen on.</param>
  135.         /// <param name="MaxClients">Maximum number of clients to allow</param>
  136.         public Server(int Port, int MaxClients)
  137.         {
  138.             this.Port = Port;
  139.             this.Listeners = new Listener[MaxClients];
  140.             this.tcpl = new TcpListener(IPAddress.Any, Port);
  141.             this.tcpl.Start();
  142.         }
  143.  
  144.         /// <summary>
  145.         /// Sets up a new listening port and lets you choose
  146.         /// whether to launch the ClientAcceptor emmediately.
  147.         /// </summary>
  148.         /// <param name="Port">Port to listen on.</param>
  149.         /// <param name="MaxClients">Maximum number of clients to allow</param>
  150.         /// <param name="LaunchAcceptClients">Emmediately launch ClientAcceptor or not.</param>
  151.         public Server(int Port, int MaxClients, bool LaunchAcceptClients)
  152.         {
  153.             this.Port = Port;
  154.             this.Listeners = new Listener[MaxClients];
  155.             this.tcpl = new TcpListener(IPAddress.Any, Port);
  156.             this.tcpl.Start();
  157.  
  158.             if (LaunchAcceptClients)
  159.                 AcceptClients();
  160.         }
  161.  
  162.         private bool AcceptNewClients = false;
  163.         /// <summary>
  164.         /// Launches the ClientAcceptor.
  165.         /// </summary>
  166.         public void AcceptClients()
  167.         {
  168.             this.AcceptNewClients = true;
  169.             this.CAT = new Thread(new ThreadStart(_ClientAcceptor));
  170.             this.CAT.Start();
  171.         }
  172.  
  173.         private void _ClientAcceptor()
  174.         {
  175.             while (this.AcceptNewClients)
  176.             {
  177.                 TcpClient newclient = this.tcpl.AcceptTcpClient();
  178.                 this.Listeners[this.ClientsCount] = new Listener(ref newclient, this.ClientsCount);
  179.                 this.Listeners[this.ClientsCount].Rec += new EventHandler<RecEventArgs>(LRec);
  180.                 this.Listeners[this.ClientsCount].Send(Encoding.ASCII.GetBytes("Hello Client!"));
  181.                 ClientsCount++;
  182.             }
  183.         }
  184.  
  185.         void LRec(object sender, Server.RecEventArgs e)
  186.         {
  187.             Rec(this, e);
  188.         }
  189.  
  190.         /// <summary>
  191.         /// Sends a preset buffer to the client.
  192.         /// </summary>
  193.         /// <param name="Data">The buffer to send.</param>
  194.         /// <param name="ClientID">The Client to send to.</param>
  195.         public void SendToClient(byte[] Data, int ClientID)
  196.         {
  197.             this.Listeners[ClientID].Send(Data);
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement