title: Async socket server with multiple clients
body: <p>i have a problem i am unable to resolve as my c# knowledge is not very good.
  I found some code on the internet and modified it according to my needs.</p>
  
  <p>My problem is that when i send messages to clients only one receives the message, then the next message is received by another client and so on.
  I want to send same message to all connected clients without losing any data.</p>
  
  <p>Server</p>
  
  <pre><code>using System;
  using System.Net.Sockets;
  using System.Threading;
  
  
  public class AsynchIOServer
  {
      static TcpListener tcpListener = new TcpListener(10);
  
      static void Listeners()
      {
  
          Socket socketForClient = tcpListener.AcceptSocket();
          if (socketForClient.Connected)
          {
              Console.WriteLine("Client:"+socketForClient.RemoteEndPoint+" now connected to server.");
              NetworkStream networkStream = new NetworkStream(socketForClient);
              System.IO.StreamWriter streamWriter =
              new System.IO.StreamWriter(networkStream);
              System.IO.StreamReader streamReader =
              new System.IO.StreamReader(networkStream);
  
              ////here we send message to client
              while (true){
                  Console.WriteLine("type your message to be recieved by client:");
                  string theString = Console.ReadLine();
                  streamWriter.WriteLine(theString);
                  ////Console.WriteLine(theString);
                  streamWriter.Flush();
              }
              streamReader.Close();
              networkStream.Close();
              streamWriter.Close();
  
          }
          socketForClient.Close();
          Console.WriteLine("Press any key to exit from server program");
          Console.ReadKey();
      }
  
      public static void Main()
      {
          //TcpListener tcpListener = new TcpListener(10);
          tcpListener.Start();
          Console.WriteLine("************This is Server program************");
          Console.WriteLine("Hoe many clients are going to connect to this server?:");
          int numberOfClientsYouNeedToConnect =int.Parse( Console.ReadLine());
          for (int i = 0; i &lt; numberOfClientsYouNeedToConnect; i++)
          {
              Thread newThread = new Thread(new ThreadStart(Listeners));
              newThread.Start();
          }
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>using System;
  using System.Net.Sockets;
  using System.Threading;
  public class Client
  {
      static public void Main(string[] Args)
      {
          TcpClient socketForServer;
          try
          {
              socketForServer = new TcpClient("localHost", 10);
          }
          catch
          {
              Console.WriteLine(
              "Failed to connect to server at {0}:999", "localhost");
              return;
          }
  
          NetworkStream networkStream = socketForServer.GetStream();
          System.IO.StreamReader streamReader =
          new System.IO.StreamReader(networkStream);
          System.IO.StreamWriter streamWriter =
          new System.IO.StreamWriter(networkStream);
          Console.WriteLine("*******This is client program who is connected to localhost on port No:10*****");
  
              try
              {
                  string outputString;
              // read the data from the host and display it
              {
                  while (true)
                  {
                      outputString = streamReader.ReadLine();
                      Console.WriteLine("Message Recieved by server:" + outputString);
  
                      streamWriter.Flush();
                  }
              }
          }
              catch
              {
                  Console.WriteLine("Exception reading from Server");
              }
          // tidy up
          networkStream.Close();
          Console.WriteLine("Press any key to exit from client program");
          Console.ReadKey();
      }
  
      private static string GetData()
      {
          //Ack from sql server
          return "ack";
      }
  }
  </code></pre>
  
  <p>Thank you</p>