Advertisement
user366312

ClientClass.cs

May 4th, 2019
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace MyClientServerLib
  11. {
  12.     public class ClientClass
  13.     {
  14.         public string Key { get; set; }
  15.         public string Value { get; set; }
  16.  
  17.         private string Host { get; set; }
  18.         private int Port { get; set; }
  19.  
  20.         private bool IsConnected = false;
  21.        
  22.         public string ID { get; private set; }
  23.         public TcpClient Tcp { get; private set; }
  24.  
  25.         private BinaryReader reader;
  26.         private BinaryWriter writer;
  27.  
  28.         public ClientClass()
  29.         {
  30.             Random rnd = new Random();
  31.             ID = AlphaNumRandom.GenerateUpperCaseString(5, rnd);
  32.         }
  33.  
  34.         public ClientClass(TcpListener listener)
  35.         {
  36.             Tcp  = listener.AcceptTcpClient();
  37.  
  38.             Host = ((IPEndPoint)Tcp.Client.RemoteEndPoint).Address.ToString();
  39.             Port = ((IPEndPoint)Tcp.Client.LocalEndPoint).Port;
  40.  
  41.             // first sent string is client ID
  42.             ID = (new BinaryReader(Tcp.GetStream())).ReadString();
  43.  
  44.             IsConnected = true;
  45.  
  46.             NetworkStream stream = Tcp.GetStream();
  47.             reader = new BinaryReader(stream);
  48.             writer = new BinaryWriter(stream);
  49.  
  50.             Console.WriteLine("Client [{0}] is now connected.", ID);
  51.  
  52.         }
  53.  
  54.         public ClientClass(string host, int port)
  55.         {
  56.             Random rnd = new Random();
  57.             ID = AlphaNumRandom.GenerateUpperCaseString(5, rnd);
  58.             Host = host;
  59.             Port = port;
  60.         }
  61.  
  62.         public void Write(string str)
  63.         {
  64.             if (IsConnected)
  65.             {
  66.                 writer.Write(str);
  67.                 writer.Flush();
  68.             }
  69.             else
  70.             {
  71.                 throw new Exception("Client " + ID + " is not connected!");
  72.             }
  73.         }
  74.  
  75.         public string ReadString()
  76.         {
  77.             if (IsConnected)
  78.             {
  79.                 return reader.ReadString();
  80.             }
  81.             else
  82.             {
  83.                 throw new Exception("Client " + ID + " is not connected!");
  84.             }
  85.         }
  86.  
  87.         public void PrintID()
  88.         {
  89.             Console.WriteLine("Client ID = {0}", ID);
  90.         }
  91.  
  92.         public void SendIdToServer()
  93.         {
  94.             if (IsConnected == true)
  95.             {
  96.                 writer.Write(ID);
  97.             }
  98.             else
  99.             {
  100.                 throw new Exception("Client " + ID + " is not connected!");
  101.             }
  102.         }
  103.  
  104.         public bool Connect()
  105.         {
  106.             if (IsConnected == false)
  107.             {
  108.                 Console.WriteLine("Client [{0}] is now connected.", ID);
  109.  
  110.                 IsConnected = true;
  111.  
  112.                 Tcp = new TcpClient(Host, Port);
  113.  
  114.                 NetworkStream stream = Tcp.GetStream();
  115.                 reader = new BinaryReader(stream);
  116.                 writer = new BinaryWriter(stream);
  117.  
  118.                 return true;
  119.             }
  120.  
  121.             return false;
  122.         }
  123.  
  124.         public bool Disconnect()
  125.         {
  126.             if (IsConnected)
  127.             {
  128.                 if (Tcp != null)
  129.                 {
  130.                     Tcp.Close();
  131.                     Tcp = null;
  132.  
  133.                     Console.WriteLine("Client [{0}] is now disconnected.", ID);
  134.  
  135.                     return true;
  136.                 }
  137.             }
  138.  
  139.             return false;
  140.         }
  141.  
  142.         public static void WriteToServer(ClientClass client)
  143.         {
  144.             try
  145.             {
  146.                 while (client.Tcp.Connected)
  147.                 {
  148.                     Console.Write("Client prompt (\"?\" for help) # ");
  149.                     string str = Console.ReadLine();
  150.                     client.Write(str);
  151.  
  152.                     switch (str)
  153.                     {
  154.                         case Commands.Help:
  155.                             Console.WriteLine("add = add key value\n ls = list keys\n upd = update key value\n exit=terminate program");
  156.                             break;
  157.  
  158.                         case Commands.AddKeyValue:
  159.                             Console.WriteLine("Enter key : ");
  160.                             string key = Console.ReadLine();
  161.                             Console.WriteLine("Enter value : ");
  162.                             string value = Console.ReadLine();
  163.                             client.Write(key);
  164.                             client.Write(value);
  165.                             break;
  166.  
  167.                         case Commands.UpdateValue:
  168.                             Console.WriteLine("Enter key : ");
  169.                             string keyToUpd = Console.ReadLine();
  170.                             Console.WriteLine("Enter val : ");
  171.                             string valToUpd = Console.ReadLine();
  172.                             client.Write(keyToUpd);
  173.                             client.Write(valToUpd);
  174.                             break;
  175.  
  176.                         case Commands.ClearScreen:
  177.                             Console.Clear();
  178.                             break;
  179.                     }
  180.                 }
  181.             }
  182.             catch
  183.             {
  184.                 client.Disconnect();
  185.             }
  186.         }
  187.  
  188.         #region read from server
  189.         public static void ReadFromServer(ClientClass client)
  190.         {
  191.             try
  192.             {
  193.                 while (client.Tcp.Connected)
  194.                 {
  195.                     string str = client.ReadString();
  196.                     Console.WriteLine(str);
  197.                 }
  198.             }
  199.             catch
  200.             {
  201.                 client.Disconnect();
  202.             }
  203.         }
  204.         #endregion
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement