title: C# TCP/IP simple chat with multiple-clients
body: <p>I'm learning c# socket programming. So, I decided to make a TCP chat, the basic idea is that A client send data to the server, then the server broadcast it for all the clients online (in this case all the clients are in a dictionary).</p>
  
  <p>When there is 1 client connected, it works as expected, the problem is occurred when there is more than 1 client connected.</p>
  
  <p><strong>Server:</strong></p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Dictionary&lt;int,TcpClient&gt; list_clients = new Dictionary&lt;int,TcpClient&gt; ();
  
          int count = 1;
  
  
          TcpListener ServerSocket = new TcpListener(IPAddress.Any, 5000);
          ServerSocket.Start();
  
          while (true)
          {
              TcpClient client = ServerSocket.AcceptTcpClient();
              list_clients.Add(count, client);
              Console.WriteLine("Someone connected!!");
              count++;
              Box box = new Box(client, list_clients);
  
              Thread t = new Thread(handle_clients);
              t.Start(box);
          }
  
      }
  
      public static void handle_clients(object o)
      {
          Box box = (Box)o;
          Dictionary&lt;int, TcpClient&gt; list_connections = box.list;
  
          while (true)
          {
              NetworkStream stream = box.c.GetStream();
              byte[] buffer = new byte[1024];
              int byte_count = stream.Read(buffer, 0, buffer.Length);
              byte[] formated = new Byte[byte_count];
              //handle  the null characteres in the byte array
              Array.Copy(buffer, formated, byte_count);
              string data = Encoding.ASCII.GetString(formated);
              broadcast(list_connections, data);
              Console.WriteLine(data);
  
          } 
      }
  
      public static void broadcast(Dictionary&lt;int,TcpClient&gt; conexoes, string data)
      {
          foreach(TcpClient c in conexoes.Values)
          {
              NetworkStream stream = c.GetStream();
  
              byte[] buffer = Encoding.ASCII.GetBytes(data);
              stream.Write(buffer,0, buffer.Length);
          }
      }
  
  }
  class Box
  {
      public TcpClient c;
       public Dictionary&lt;int, TcpClient&gt; list;
  
      public Box(TcpClient c, Dictionary&lt;int, TcpClient&gt; list)
      {
          this.c = c;
          this.list = list;
      }
  
  }
  </code></pre>
  
  <p>I created this box, so I can pass 2 args for the <code>Thread.start()</code>.</p>
  
  <p><strong>Client:</strong></p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          IPAddress ip = IPAddress.Parse("127.0.0.1");
          int port = 5000;
          TcpClient client = new TcpClient();
          client.Connect(ip, port);
          Console.WriteLine("client connected!!");
          NetworkStream ns = client.GetStream();
  
          string s;
          while (true)
          {
               s = Console.ReadLine();
              byte[] buffer = Encoding.ASCII.GetBytes(s);
              ns.Write(buffer, 0, buffer.Length);
              byte[] receivedBytes = new byte[1024];
              int byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length);
              byte[] formated = new byte[byte_count];
              //handle  the null characteres in the byte array
              Array.Copy(receivedBytes, formated, byte_count); 
              string data = Encoding.ASCII.GetString(formated);
              Console.WriteLine(data);
          }
          ns.Close();
          client.Close();
          Console.WriteLine("disconnect from server!!");
          Console.ReadKey();        
      }
  }
  </code></pre>