C# - Client/Server Broadcast Example 1

This code snippet demonstrates how to broadcast messages to all connected clients. The server listens for incoming connections. Each connection is handled by a new thread. The server maintains a list of all connected clients in an array. When a client sends a message to the server, the thread responsible for that client then sends the message to all clients except for it's self. Run the server first then load multiple client instances.

Client

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Net;  
  4. using System.Net.Sockets;  
  5. using System.IO;  
  6. using System.Text;  
  7. using System.Threading;  
  8.  
  9. namespace Client  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             try  
  16.             {  
  17.                 TcpClient tcpClient = new TcpClient("127.0.0.1", 1234);  
  18.                 Console.WriteLine("Connected to server.");  
  19.                 Console.WriteLine("");  
  20.  
  21.                 Thread thread = new Thread(Read);  
  22.                 thread.Start(tcpClient);  
  23.  
  24.                 StreamWriter sWriter = new StreamWriter(tcpClient.GetStream());  
  25.  
  26.                 while (true)  
  27.                 {  
  28.                     if (tcpClient.Connected)  
  29.                     {  
  30.                         string input = Console.ReadLine();  
  31.                         sWriter.WriteLine(input);  
  32.                         sWriter.Flush();  
  33.                     }  
  34.                 }  
  35.  
  36.             }  
  37.             catch (Exception e)  
  38.             {  
  39.                 Console.Write(e.Message);  
  40.             }  
  41.  
  42.             Console.ReadKey();  
  43.         }  
  44.  
  45.         static void Read(object obj)  
  46.         {  
  47.             TcpClient tcpClient = (TcpClient)obj;  
  48.             StreamReader sReader = new StreamReader(tcpClient.GetStream());  
  49.  
  50.             while (true)  
  51.             {  
  52.                 try  
  53.                 {  
  54.                     string message = sReader.ReadLine();  
  55.                     Console.WriteLine(message);  
  56.                 }  
  57.                 catch (Exception e)  
  58.                 {  
  59.                     Console.WriteLine(e.Message);  
  60.                     break;  
  61.                 }  
  62.             }  
  63.         }  
  64.     }  
  65. } 

Server

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Net;  
  4. using System.Net.Sockets;  
  5. using System.IO;  
  6. using System.Text;  
  7. using System.Threading;  
  8.  
  9. namespace Server  
  10. {  
  11.     class Program  
  12.     {  
  13.         private static TcpListener tcpListener;  
  14.         private static List<TcpClient> tcpClientsList = new List<TcpClient>();  
  15.  
  16.         static void Main(string[] args)  
  17.         {  
  18.             tcpListener = new TcpListener(IPAddress.Any, 1234);  
  19.             tcpListener.Start();  
  20.  
  21.             Console.WriteLine("Server started");  
  22.  
  23.             while (true)  
  24.             {  
  25.                 TcpClient tcpClient = tcpListener.AcceptTcpClient();  
  26.                 tcpClientsList.Add(tcpClient);  
  27.  
  28.                 Thread thread = new Thread(ClientListener);  
  29.                 thread.Start(tcpClient);  
  30.             }  
  31.         }  
  32.  
  33.         public static void ClientListener(object obj)  
  34.         {  
  35.             TcpClient tcpClient = (TcpClient)obj;  
  36.             StreamReader reader = new StreamReader(tcpClient.GetStream());  
  37.  
  38.             Console.WriteLine("Client connected");  
  39.  
  40.             while (true)  
  41.             {  
  42.                 string message = reader.ReadLine();  
  43.                 BroadCast(message, tcpClient);  
  44.                 Console.WriteLine(message);  
  45.             }  
  46.         }  
  47.  
  48.         public static void BroadCast(string msg, TcpClient excludeClient)  
  49.         {  
  50.             foreach (TcpClient client in tcpClientsList)  
  51.             {  
  52.                 if (client != excludeClient)  
  53.                 {  
  54.                     StreamWriter sWriter = new StreamWriter(client.GetStream());  
  55.                     sWriter.WriteLine(msg);  
  56.                     sWriter.Flush();  
  57.                 }  
  58.             }  
  59.         }  
  60.     }  
  61. } 

2 comments:

Unknown said...

Thank you very much! This is what I want! I so appreciate it!

Shivangi Rathore said...

Nice article!!
Have a look Bulk sms services

Post a Comment