Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Net.Sockets;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1
- {
- class Program
- {
- public static Hashtable clientsList = new Hashtable();
- static void Main(string[] args)
- {
- int port = 69;
- TcpListener serverSocket = new TcpListener(port);
- TcpClient clientSocket = default(TcpClient);
- int counter = 0;
- serverSocket.Start();
- Console.BackgroundColor = ConsoleColor.Gray;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.Write(" ");
- Console.Write(" Temple Chat Server Authors: James Hine ");
- Console.Write(" Version 1.0a Jak Clark ");
- Console.Write(" ");
- Console.ResetColor();
- Console.Write(" ");
- Console.WriteLine(" [{0}] Server initialised.", DateTime.Now);
- Console.WriteLine(" [{0}] Listening on port {1}.", DateTime.Now, port);
- counter = 0;
- while ((true))
- {
- counter += 1;
- clientSocket = serverSocket.AcceptTcpClient();
- byte[] bytesFrom = new byte[10025];
- string dataFromClient = null;
- NetworkStream networkStream = clientSocket.GetStream();
- networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
- dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
- dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
- clientsList.Add(dataFromClient, clientSocket);
- broadcast(dataFromClient + " Joined ", dataFromClient, false);
- Console.WriteLine(" [{0}] User '{1}' joined.", DateTime.Now, dataFromClient);
- handleClient client = new handleClient();
- client.startClient(clientSocket, dataFromClient, clientsList);
- }
- clientSocket.Close();
- serverSocket.Stop();
- Console.WriteLine(" [{0}] Server closed.", DateTime.Today);
- Console.ReadLine();
- }
- public static void broadcast(string msg, string uName, bool flag)
- {
- foreach (DictionaryEntry Item in clientsList)
- {
- TcpClient broadcastSocket;
- broadcastSocket = (TcpClient)Item.Value;
- NetworkStream broadcastStream = broadcastSocket.GetStream();
- Byte[] broadcastBytes = null;
- if (flag == true)
- {
- broadcastBytes = Encoding.ASCII.GetBytes(uName + " says: " + msg);
- }
- else
- {
- broadcastBytes = Encoding.ASCII.GetBytes(msg);
- }
- broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
- broadcastStream.Flush();
- }
- }
- }
- public class handleClient
- {
- TcpClient clientSocket;
- string clNo;
- Hashtable clientsList;
- public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
- {
- this.clientSocket = inClientSocket;
- this.clNo = clineNo;
- this.clientsList = cList;
- Thread ctThread = new Thread(doChat);
- ctThread.Start();
- }
- private void doChat()
- {
- int requestCount = 0;
- byte[] bytesFrom = new byte[10025];
- string dataFromClient = null;
- Byte[] sendBytes = null;
- string serverResponse = null;
- string rCount = null;
- requestCount = 0;
- while ((true))
- {
- try
- {
- requestCount = requestCount + 1;
- NetworkStream networkStream = clientSocket.GetStream();
- networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
- dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
- dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
- Console.WriteLine(" [{0}] {1} -> Server: {2}", DateTime.Now, clNo, dataFromClient);
- rCount = Convert.ToString(requestCount);
- Program.broadcast(dataFromClient, clNo, true);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment