i want to send a private message to particular client by selecting number in comboBox. but had tried to code it but message still end up receive the message at every clients side that connected to server. i want to send the message using the worker socket object stored in ArrayList, but don't know how to get it correct.

Please help. Thank you very much.
here is the code i tried, but doesn't work

void indmsgbtn_Click(object sender, EventArgs e)
 {
     try
     {
         string msg = richtxtindmsg.Text;
         msg = "Private Admin Message: " + msg + "\n";
         byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);
         Socket workerSocket = null;
         Convert.ToInt32(comboBox1.SelectedItem);
         
         if (comboBox1.SelectedIndex != 0)
         {
             MessageBox.Show("Please Choose a Connected User no.");
             return;
         }
         for (int i = 0; i < m_workerSocketList.Count; i++)
         {
             comboBox1.SelectedItem = m_workerSocketList[i];
            // workerSocket = (Socket)m_workerSocketList[i];
             if (workerSocket != null)
             {
                 if (workerSocket.Connected)
                 {
                     workerSocket.Send(byData);
                 }
             }
         }  
     }
     catch (SocketException se)
     {
         MessageBox.Show(se.Message);
     }
 }

Update:

i tried to modify the codes, but still have problem, still unable to send private message to selected client number number

i already set that client number will be added in the comboBox each time each client connect to server.

void indmsgbtn_Click(object sender, EventArgs e)
        {
            try
            {
  
                string msg = richtxtindmsg.Text;
                msg = "Private Admin Message: " + msg + "\n";
                byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);
                Socket workerSocket = null;
                for (int i = 0; i < m_workerSocketList.Count; i++)
                {
                   // workerSocket = (Socket)m_workerSocketList[i]; 
              //this is the code that allow send message to all the client that connected

                    comboBox1.SelectedItem == m_workerSocketList[i];
                    if (workerSocket != null)
                    {
                        if (workerSocket.Connected)
                        {
                            workerSocket.Send(byData);
                        }
                    }
                }
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }

Please help, thank you very much.

UPDATE again:

i modified the code, i manage to send private message to particular client, but some problems arise, first, total number of messages received by particular client depends on how many clients had connected to server.
For example, if there are 3 clients connect to server, so the index client will be no. 1, 2, 3. Assume i send private a message 'Hello' to client no.2 (choose number 2 in comboBox), but since there are total 3 clients connected, as the result client no.2 will receive 3 'Hello' messages at once.

Another problem is server system will become error when i want to send private message if any one particular client disconnect connection and reconnect again.

please help. really had no clue how to fix it. thank you very much.
here is the code:

void indmsgbtn_Click(object sender, EventArgs e)
 {
     try
     {
         string msg = richtxtindmsg.Text;
         msg = "Private Admin Message: " + msg + "\n";
         byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);
         Socket workerSocket = null;
         for (int i = 0; i < m_workerSocketList.Count; i++)
         {   
             //this is the code to send msg to all connected clients
             //workerSocket = (Socket)m_workerSocketList[i];
  
             Socket workerSocket = (Socket)m_workerSocketList[comboBox1.SelectedIndex];
             
             if (workerSocket != null)
             {   
                 if (workerSocket.Connected)
                 {
                     workerSocket.Send(byData);
                 }
             }
         }
     }
     catch (SocketException se)
     {
         MessageBox.Show(se.Message);
     }
 }

update again:

i modified the code again, but still enable to solve the problem that Cannot send private message anymore once there is client disconnect and reconnect.

anyone know whats wrong with my code please help. thanks.

void indmsgbtn_Click(object sender, EventArgs e)
{
    try
    {    
    string msg = richtxtindmsg.Text;            
    msg = "Private Admin Message: " + msg + "\n";
    byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);
    Socket workerSocket = null;
   // for (int i = 0; i < m_workerSocketList.Count; i++)
    //{
      Socket workerSocket = (Socket)m_workerSocketList[comboBox1.SelectedIndex];
      if (workerSocket != null)
      {
          if (workerSocket.Connected)
          {
                  try
                  {
                      workerSocket.Send(byData);
                  }
                  catch (SocketException ex)
                  {
                      MessageBox.Show(ex.Message);
                  }
              }
      }
   } 
    catch (SocketException se)
    {
           MessageBox.Show(se.Message);
    }
}
This article has been dead for over six months. Start a new discussion instead.