Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace MyClientServerLib
- {
- public class ClientClass
- {
- public string Key { get; set; }
- public string Value { get; set; }
- private string Host { get; set; }
- private int Port { get; set; }
- private bool IsConnected = false;
- public string ID { get; private set; }
- public TcpClient Tcp { get; private set; }
- private BinaryReader reader;
- private BinaryWriter writer;
- public ClientClass()
- {
- Random rnd = new Random();
- ID = AlphaNumRandom.GenerateUpperCaseString(5, rnd);
- }
- public ClientClass(TcpListener listener)
- {
- Tcp = listener.AcceptTcpClient();
- Host = ((IPEndPoint)Tcp.Client.RemoteEndPoint).Address.ToString();
- Port = ((IPEndPoint)Tcp.Client.LocalEndPoint).Port;
- // first sent string is client ID
- ID = (new BinaryReader(Tcp.GetStream())).ReadString();
- IsConnected = true;
- NetworkStream stream = Tcp.GetStream();
- reader = new BinaryReader(stream);
- writer = new BinaryWriter(stream);
- Console.WriteLine("Client [{0}] is now connected.", ID);
- }
- public ClientClass(string host, int port)
- {
- Random rnd = new Random();
- ID = AlphaNumRandom.GenerateUpperCaseString(5, rnd);
- Host = host;
- Port = port;
- }
- public void Write(string str)
- {
- if (IsConnected)
- {
- writer.Write(str);
- writer.Flush();
- }
- else
- {
- throw new Exception("Client " + ID + " is not connected!");
- }
- }
- public string ReadString()
- {
- if (IsConnected)
- {
- return reader.ReadString();
- }
- else
- {
- throw new Exception("Client " + ID + " is not connected!");
- }
- }
- public void PrintID()
- {
- Console.WriteLine("Client ID = {0}", ID);
- }
- public void SendIdToServer()
- {
- if (IsConnected == true)
- {
- writer.Write(ID);
- }
- else
- {
- throw new Exception("Client " + ID + " is not connected!");
- }
- }
- public bool Connect()
- {
- if (IsConnected == false)
- {
- Console.WriteLine("Client [{0}] is now connected.", ID);
- IsConnected = true;
- Tcp = new TcpClient(Host, Port);
- NetworkStream stream = Tcp.GetStream();
- reader = new BinaryReader(stream);
- writer = new BinaryWriter(stream);
- return true;
- }
- return false;
- }
- public bool Disconnect()
- {
- if (IsConnected)
- {
- if (Tcp != null)
- {
- Tcp.Close();
- Tcp = null;
- Console.WriteLine("Client [{0}] is now disconnected.", ID);
- return true;
- }
- }
- return false;
- }
- public static void WriteToServer(ClientClass client)
- {
- try
- {
- while (client.Tcp.Connected)
- {
- Console.Write("Client prompt (\"?\" for help) # ");
- string str = Console.ReadLine();
- client.Write(str);
- switch (str)
- {
- case Commands.Help:
- Console.WriteLine("add = add key value\n ls = list keys\n upd = update key value\n exit=terminate program");
- break;
- case Commands.AddKeyValue:
- Console.WriteLine("Enter key : ");
- string key = Console.ReadLine();
- Console.WriteLine("Enter value : ");
- string value = Console.ReadLine();
- client.Write(key);
- client.Write(value);
- break;
- case Commands.UpdateValue:
- Console.WriteLine("Enter key : ");
- string keyToUpd = Console.ReadLine();
- Console.WriteLine("Enter val : ");
- string valToUpd = Console.ReadLine();
- client.Write(keyToUpd);
- client.Write(valToUpd);
- break;
- case Commands.ClearScreen:
- Console.Clear();
- break;
- }
- }
- }
- catch
- {
- client.Disconnect();
- }
- }
- #region read from server
- public static void ReadFromServer(ClientClass client)
- {
- try
- {
- while (client.Tcp.Connected)
- {
- string str = client.ReadString();
- Console.WriteLine(str);
- }
- }
- catch
- {
- client.Disconnect();
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement