daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Threading;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Collections;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         public static Hashtable clientsList = new Hashtable();
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             int port = 69;
  16.             TcpListener serverSocket = new TcpListener(port);
  17.             TcpClient clientSocket = default(TcpClient);
  18.             int counter = 0;
  19.  
  20.            
  21.  
  22.             serverSocket.Start();
  23.             Console.BackgroundColor = ConsoleColor.Gray;
  24.             Console.ForegroundColor = ConsoleColor.Black;
  25.             Console.Write("                                                                                ");
  26.             Console.Write("   Temple Chat Server                                     Authors: James Hine   ");
  27.             Console.Write("   Version 1.0a                                                    Jak Clark    ");
  28.             Console.Write("                                                                                ");
  29.             Console.ResetColor();
  30.             Console.Write("                                                                                ");
  31.             Console.WriteLine("   [{0}] Server initialised.", DateTime.Now);
  32.             Console.WriteLine("   [{0}] Listening on port {1}.", DateTime.Now, port);
  33.             counter = 0;
  34.             while ((true))
  35.             {
  36.                 counter += 1;
  37.                 clientSocket = serverSocket.AcceptTcpClient();
  38.  
  39.                 byte[] bytesFrom = new byte[10025];
  40.                 string dataFromClient = null;
  41.  
  42.                 NetworkStream networkStream = clientSocket.GetStream();
  43.                 networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
  44.                 dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
  45.                 dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
  46.  
  47.                 clientsList.Add(dataFromClient, clientSocket);
  48.  
  49.                 broadcast(dataFromClient + " Joined ", dataFromClient, false);
  50.  
  51.                 Console.WriteLine("   [{0}] User '{1}' joined.", DateTime.Now, dataFromClient);
  52.                 handleClient client = new handleClient();
  53.                 client.startClient(clientSocket, dataFromClient, clientsList);
  54.             }
  55.  
  56.             clientSocket.Close();
  57.             serverSocket.Stop();
  58.             Console.WriteLine("   [{0}] Server closed.", DateTime.Today);
  59.             Console.ReadLine();
  60.         }
  61.  
  62.         public static void broadcast(string msg, string uName, bool flag)
  63.         {
  64.             foreach (DictionaryEntry Item in clientsList)
  65.             {
  66.                 TcpClient broadcastSocket;
  67.                 broadcastSocket = (TcpClient)Item.Value;
  68.                 NetworkStream broadcastStream = broadcastSocket.GetStream();
  69.                 Byte[] broadcastBytes = null;
  70.  
  71.                 if (flag == true)
  72.                 {
  73.                     broadcastBytes = Encoding.ASCII.GetBytes(uName + " says: " + msg);
  74.                 }
  75.                 else
  76.                 {
  77.                     broadcastBytes = Encoding.ASCII.GetBytes(msg);
  78.                 }
  79.  
  80.                 broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
  81.                 broadcastStream.Flush();
  82.             }
  83.         }  
  84.     }
  85.  
  86.  
  87.     public class handleClient
  88.     {
  89.         TcpClient clientSocket;
  90.         string clNo;
  91.         Hashtable clientsList;
  92.  
  93.         public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
  94.         {
  95.             this.clientSocket = inClientSocket;
  96.             this.clNo = clineNo;
  97.             this.clientsList = cList;
  98.             Thread ctThread = new Thread(doChat);
  99.             ctThread.Start();
  100.         }
  101.  
  102.         private void doChat()
  103.         {
  104.             int requestCount = 0;
  105.             byte[] bytesFrom = new byte[10025];
  106.             string dataFromClient = null;
  107.             Byte[] sendBytes = null;
  108.             string serverResponse = null;
  109.             string rCount = null;
  110.             requestCount = 0;
  111.  
  112.             while ((true))
  113.             {
  114.                 try
  115.                 {
  116.                     requestCount = requestCount + 1;
  117.                     NetworkStream networkStream = clientSocket.GetStream();
  118.                     networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
  119.                     dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
  120.                     dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
  121.                     Console.WriteLine("   [{0}] {1} -> Server: {2}", DateTime.Now, clNo, dataFromClient);
  122.                     rCount = Convert.ToString(requestCount);
  123.  
  124.                     Program.broadcast(dataFromClient, clNo, true);
  125.                 }
  126.                 catch (Exception ex)
  127.                 {
  128.                     Console.WriteLine(ex.ToString());
  129.                 }
  130.             }
  131.         }
  132.     }
  133. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top