Send Message To Specific Client By Selecting Number In ComboBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haanjae
    New Member
    • Mar 2011
    • 13

    Send Message To Specific Client By Selecting Number In ComboBox

    I want to send a private message to particular client by selecting number in comboBox. but had tried to code it but message still receive at every clients 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.
    Any help will be appreciated, thanks.

    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;
             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);
         }
     }
  • Paul Johnson
    New Member
    • Oct 2010
    • 97

    #2
    You might want to include a UID onto the message or send it to a specific IP address.

    Comment

    • haanjae
      New Member
      • Mar 2011
      • 13

      #3
      thanks for reply. ya, that is why i want to use the number 1 until 9 in combobox to match the ID of client, but don't know how to code it correctly.

      Comment

      • haanjae
        New Member
        • Mar 2011
        • 13

        #4
        update:
        i tried to modify the codes again, 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.

        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++)
                        {
                           // 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);
                    }
                }

        Comment

        Working...