// See https://aka.ms/new-console-template for more information using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; class TcpServer { private static TcpListener server; private static bool isRunning = true; private static List connectedClients = new List(); static void Main(string[] args) { server = new TcpListener(IPAddress.Any, 5000); server.Start(); Console.WriteLine("Server started on port 5000."); while (isRunning) { var client = server.AcceptTcpClient(); Console.WriteLine("Client connected!"); lock (connectedClients) { connectedClients.Add(client); // Add new client to the list } // Handle each client connection in a new thread Thread clientThread = new Thread(HandleClient); clientThread.Start(client); } } private static void HandleClient(object obj) { TcpClient client = (TcpClient)obj; NetworkStream stream = client.GetStream(); byte[] buffer = new byte[4096]; int bytesRead; try { while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0) { string receivedData = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine("Received: " + receivedData); // Split the message into command and data string[] messageParts = receivedData.Split(new char[] { ':' }, 2); if (messageParts.Length < 2) continue; // Ignore malformed messages string command = messageParts[0]; string message = messageParts[1]; switch (command) { case "PUBKEY": Console.WriteLine("Received RSA Public Key: " + message); // Broadcast the message to all connected clients (excluding the sender) BroadcastMessageToClients(client, $"{command}:{message}"); break; case "AESKEY": Console.WriteLine("Received Encrypted AES Key: " + message); // Broadcast the message to all connected clients (excluding the sender) BroadcastMessageToClients(client, $"{command}:{message}"); break; case "MSG": Console.WriteLine("Received AES Encrypted Message: " + message); // Broadcast the message to all connected clients (excluding the sender) BroadcastMessageToClients(client, $"{command}:{message}"); break; case "EXIT": Console.WriteLine("Client is disconnecting..."); client.Close(); lock (connectedClients) { connectedClients.Remove(client); // Remove client from the list } return; default: Console.WriteLine("Unknown command received: " + command); break; } } } catch (Exception ex) { Console.WriteLine("Client disconnected with error: " + ex.Message); } finally { client.Close(); lock (connectedClients) { connectedClients.Remove(client); } Console.WriteLine("Client disconnected."); } } private static void BroadcastMessageToClients(TcpClient senderClient, string message) { byte[] data = Encoding.ASCII.GetBytes("Broadcast: " + message); lock (connectedClients) { foreach (var client in connectedClients) { // Send to all clients except the sender if (client != senderClient) { try { NetworkStream stream = client.GetStream(); stream.Write(data, 0, data.Length); } catch (Exception ex) { Console.WriteLine("Error sending to client: " + ex.Message); } } } } } }