c#tcp radio application

时间:2016-01-04 11:45:14

标签: c# tcp tcpclient

我有一个tcp应用程序 我需要做的是设置几个实例(完成)并向每个服务器发送命令。我希望在给定特定命令时,服务器将侦听网络上的任何操作并报告数据,并且仍然能够将命令发送到此服务器或其他服务器。 我的代码: radio instancese:

  instances = new radio1[list.Count];
                            for (int j = 0; j < list.Count; j++)
                            {
                                instances[j] = new radio1(list[j].Key, list[j].Value);
                                // MessageBox.Show(instances[j].getIP());
                            }

发送命令

 while ((line = sr.ReadLine()) != null)
                        {
                            var splitinig = line.Split(',');

                            // MessageBox.Show(splitinig.Length.ToString());
                            for (int i = 0; i < instances.Length; i++)
                            {
                              //  instances[i].sendCMD(listen);
                                if (splitinig[0] == instances[i].getRadio())
                                {
                                    if (splitinig[1].ToString() == "ptt1")
                                        instances[i].sendCMD(ptt1, splitinig[2].ToString());
                                    else if ((splitinig[1].ToString() == "ptt2"))
                                        instances[i].sendCMD(ptt2, splitinig[2].ToString());
                                    else
                                        instances[i].sendCMD(ptt0);
                                }
                            }

                            //MessageBox.Show(sw.Elapsed.ToString());
                        }
                        for (int i = 0; i < instances.Length; i++)
                        {
                            instances[i].sendCMD(stoplisten);
                            instances[i].closeConnection();
                        }

无线电类发送命令:

 public void sendCMD(string cmd)
        {

                //String str = ;
                Stream stm = tcpclnt.GetStream();

                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(cmd);
                Console.WriteLine("Transmitting.....");

                stm.Write(ba, 0, ba.Length);
                /*   if (cmd == "$rmdbgvoicestats!")
                   {
                       bTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent1);
                       //bTimer.Interval = 100;
                       bTimer.Enabled = true;
                   }*/

                while (tcpclnt.Connected)
                {
                    byte[] bb = new byte[tcpclnt.ReceiveBufferSize];
                    int k = stm.Read(bb, 0, tcpclnt.ReceiveBufferSize);
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < k; i++)
                        sb.Append((Convert.ToChar(bb[i])));
                    Console.WriteLine(" ");

                    writeLog(cmd, sb.ToString());
                }
        }

任何人都可以告诉我如何在循环中改变这一点而不干扰程序的其余部分吗?我希望在发送其他命令时监听数据,以防没有数据发送给我

0 个答案:

没有答案