Click here to Skip to main content
14,732,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,


how to make the client to receive commands from a server asynchronously?
server is monitoring several clients,

client side code: this works only when a button is clicked in client but i want this to work automatically. where should i put this code in client side?


private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                byte[] buffer = new byte[1024];
                int iRx = m_socClient.Receive(buffer);
                char[] chars = new char[iRx];

                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
                System.String szData = new System.String(chars);
                txtDataRx.Text = szData;

                if (szData == "info")
                {
                    string[] lines1 = System.IO.File.ReadAllLines(@"D:\test55.txt");
                    // string replyMsg = "client Reply: INFO command " ;
                    // Convert the reply to byte array
                    foreach (string line in lines1)
                    {

                        byte[] byData = System.Text.Encoding.ASCII.GetBytes(line);

                        Socket workerSocket = (Socket)m_socClient;
                        workerSocket.Send(byData);
                    }

                }
                if (szData == "stop")
                {
                    string replyMsg = "client Reply: STOP command ";
                    // Convert the reply to byte array
                    byte[] byData = System.Text.Encoding.ASCII.GetBytes(replyMsg);

                    Socket workerSocket = (Socket)m_socClient;
                    workerSocket.Send(byData);
                }
             /*   if (szData == "file")
                {
                    string replyMsg = "client Reply: FILE command ";
                    // Convert the reply to byte array
                    byte[] byData = System.Text.Encoding.ASCII.GetBytes(replyMsg);

                    Socket workerSocket = (Socket)m_socClient;
                    workerSocket.Send(byData);
                }
*/            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
            

        }


Regards,
Amith
Posted
Updated 27-Mar-12 23:21pm
v2
Comments
Rahul Rajat Singh 28-Mar-12 4:22am
   
Improved code readability

1 solution

If you want to do something asynchronously, you can put it into an own thread:
Thread myThread = new Thread(ListeningToServer);
myThread.IsBackground = true;
myThread.Start();

...

private void ListeningToServer()
{
  while (true)
  {
    //Put your code here
  }
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900