title: Multiclient server-client c#
body: <p>so recently i asked a question about a problem i had in my server-client program and i could solve the problem even though the answers i got on the question didnt help at all (thanks anyway :] ), but now i have another problem, i need the program to make it a multiclient-server type of thing, and i dont really know what to do. I have doubts on the ParameterizedThreadStart, how i put it to work etc, i've searched on it, i know its a thread method but still dont know how the heck i use it, any ideas? thanks in advance.</p>
  
  <p>Client Code:</p>
  
  <pre><code>    public Form1()
      {
  
          InitializeComponent();
  
      }
  
      private void Form1_Load(object sender, EventArgs e) //connect to server
      {
          client = new TcpClient();
          IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse("192.168.254.34"), 123); // sincronizacao do IP com a porta
          try
          {
  
              Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  
              client.Connect(IP_End);
  
              if (client.Connected)
              {
  
                  STW = new StreamWriter(client.GetStream());
                  STR = new StreamReader(client.GetStream());
                  STW.AutoFlush = true;
  
                  backgroundWorker1.RunWorkerAsync(); // Começar a receber dados em background
                  backgroundWorker1.WorkerSupportsCancellation = true; // possibilidade de cancelar o fio
  
              }
              else
              {
  
                  reconnect();
  
              }
  
          }
          catch (SocketException exception)
          {
  
              MessageBox.Show(exception.Message.ToString());
              reconnect();
          }
      }
  
      public void reconnect()
      {
          try
          {
  
              IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse("192.168.254.34"), 123); // sincronizacao do IP com a porta
              client.Connect(IP_End);
  
              STW = new StreamWriter(client.GetStream());
              STR = new StreamReader(client.GetStream());
              STW.AutoFlush = true;
  
              backgroundWorker1.RunWorkerAsync(); 
              backgroundWorker1.WorkerSupportsCancellation = true; 
          }
          catch (SocketException exception)
          {
  
              MessageBox.Show(exception.Message.ToString());
              reconnect();
          }
      }
  
      private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
      {
          while(client.Connected) 
          {
              try
              {
                  receive = STR.ReadLine();
                  this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.Text=(receive + "\n\r"); }));
  
                  receive = "";
              }
              catch(Exception x)
              {
                  MessageBox.Show(x.Message.ToString());
              }
          }
      }
  }
  </code></pre>
  
  <p>Server Code:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      private TcpClient client;
      public StreamReader STR;
      public StreamWriter STW;
      public string receive;
      public String text_to_send;
  
      public Form1()
      {
  
          InitializeComponent();
  
      }
  
      private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) 
      {
          if(client.Connected) //enquanto o cliente tiver conectado pode escrever para o servidor
          {
              STW.WriteLine(text_to_send);
              this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.Text= (text_to_send + "\n\r"); }));
          }
          else
          {
              MessageBox.Show("Envio Falhado!"); 
          }
  
      }
  
      private void button1_Click(object sender, EventArgs e) 
      {
          if (text_to_send != "" &amp;&amp; textBox1.Text != "")
          {
              text_to_send = textBox1.Text;
              backgroundWorker2.RunWorkerAsync();
          }
          else {
              MessageBox.Show("Escreva uma mensagem para ser enviada");
          }
          textBox1.Text = "";
      }
  
      private void Form1_Load(object sender, EventArgs e) 
      {
          TcpListener Listener = new TcpListener(IPAddress.Any, 123); 
  
          Listener.Start();
          client = Listener.AcceptTcpClient();
  
  
  
          if (text_to_send != "")
          {
              text_to_send = "Conexão efetuada com sucesso...";
              backgroundWorker2.RunWorkerAsync();
          }
  
          STR = new StreamReader(client.GetStream());
          STW = new StreamWriter(client.GetStream());
          STW.AutoFlush = true; 
  
      }
  
  }
  </code></pre>
  
  <p>Server Code (working on it):</p>
  
  <pre><code>    public Form1()
      {
  
          InitializeComponent();
  
      }
  
      private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) 
      {
          if(client.Connected) 
          {
              STW.WriteLine(text_to_send);
              this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.Text= (text_to_send + "\n\r"); }));
          }
          else
          {
              MessageBox.Show("Envio Falhado!"); 
          }
  
      }
  
      private void button1_Click(object sender, EventArgs e) 
      {
          if (text_to_send != "" &amp;&amp; textBox1.Text != "")
          {
              text_to_send = textBox1.Text;
              backgroundWorker2.RunWorkerAsync();
          }
          else {
              MessageBox.Show("Escreva uma mensagem para ser enviada");
          }
          textBox1.Text = "";
      }
  
      private void Form1_Load(object sender, EventArgs e) // criar server
      {
  
          this.threadTcp = new Thread(new ThreadStart(ListenToClients));
  
          Listener.Start();
          client = Listener.AcceptTcpClient();
  
  
  
          if (text_to_send != "")
          {
              text_to_send = "Conexão efetuada com sucesso...";
              backgroundWorker2.RunWorkerAsync();
          }
  
          STR = new StreamReader(client.GetStream());
          STW = new StreamWriter(client.GetStream());
          STW.AutoFlush = true; 
      }
  
      private void ListenToClients()
      {
          Listener.Start();
          while (true)
          {
              try
              {
                  this.Listener.AcceptTcpClient();
                  client = Listener.AcceptTcpClient();
  
                  Thread threadClient = new Thread(new ParameterizedThreadStart(HandleClients));
                  threadClient.Start(client);
  
              }
              catch (Exception)
              {
                  MessageBox.Show("error");
              }
          }
      }
      private void HandleClients()
      {
          //TcpListener Listener = new TcpListener(IPAddress.Any, int.Parse(textBox4.Text)); 
          Listener.Start();
          client = Listener.AcceptTcpClient();
  
  
  
          if (text_to_send != "")
          {
              text_to_send = "Conexão efetuada com sucesso...";
              backgroundWorker2.RunWorkerAsync();
          }
  
          STR = new StreamReader(client.GetStream());
          STW = new StreamWriter(client.GetStream());
          STW.AutoFlush = true; 
  
      }
  
  }
  </code></pre>
  
  <p>}</p>