예제 #1
0
        private string Communication_Receive(int IP_Port)
        {
            string receiveStr = null;

            byte[] receiveData = null;

            IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, IP_Port);

            try
            {
                receiveData = client.Receive(ref remotePoint);
            }
            catch
            {
                this.Dispatcher.Invoke(
                    new Action(
                        delegate
                {
                    Infor_Text.Text += "Newwork Connection with DUT fail, Please check" + "\r\n\r\n";
                    Infor_Text.ScrollToEnd();
                }));
            }

            receiveStr  = Encoding.Default.GetString(receiveData);
            remotePoint = null;
            return(receiveStr);
        }
예제 #2
0
        private void MainThread_Windows()
        {
            while (true)
            {
                bool   IsComing = false;
                string Str      = "";
                if (PC_State == true)
                {
                    while (!IsComing)
                    {
                        try
                        {
                            Str = Communication_Receive(11111);
                            if (Str.Contains("&&"))
                            {
                                IsComing = true;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        catch
                        {
                            continue;
                        }
                    }
                    string Pre_IP = Str.Substring(Str.IndexOf("//") + 2, Str.IndexOf("&&") - Str.IndexOf("//") - 2);
                    string mode   = Str.Substring(Str.IndexOf("&&") + 2, Str.Length - Str.IndexOf("&&") - 2);
                    Str = Str.Substring(0, Str.IndexOf("//"));
                    TimeSpan nowtime;
                    this.Dispatcher.Invoke(
                        new Action(
                            delegate
                    {
                        Infor_Text.Text += Str + "\r\n";
                        Infor_Text.Text += "Communication Start..." + "(time:" + DateTime.Now.ToString("hh:mm:ss") + ")\r\n";
                        Infor_Text.ScrollToEnd();
                    })
                        );

                    if (mode.Contains("Server"))
                    {
                        KillALLProcess("", "iperf"); KillALLProcess("", "cmd");
                        Process p_server = new Process();
                        p_server.StartInfo.FileName = "cmd.exe";
                        //p_server.StartInfo.WorkingDirectory = "..\\Debug\\iperf2";
                        p_server.StartInfo.UseShellExecute        = false; //是否使用操作系统shell启动
                        p_server.StartInfo.RedirectStandardInput  = true;  //接受来自调用程序的输入信息
                        p_server.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
                        p_server.StartInfo.RedirectStandardError  = true;  //重定向标准错误输出
                        p_server.StartInfo.CreateNoWindow         = true;  //不显示程序窗口
                        p_server.Start();                                  //启动程序
                        p_server.StandardInput.WriteLine(Str);

                        // 判断服务器是否开启成功
                        string server_str = p_server.StandardOutput.ReadLine();
                        while (true)
                        {
                            string str = p_server.StandardOutput.ReadLine();
                            if (str == null)
                            {
                                break;
                            }
                            server_str += str;
                            if (server_str.Contains("failed") || server_str.Contains("listening on"))
                            {
                                break;
                            }
                        }
                        if (!server_str.Contains("listening on"))
                        {
                            KillALLProcess("", "iperf");
                        }

                        Communication_Send("PC_Over", Pre_IP, 11111);
                        //CallWithTimeout(ReadBuf, 2000);// for end
                        this.Dispatcher.Invoke(
                            new Action(
                                delegate
                        {
                            //Infor_Text.Text += Str;
                            Infor_Text.Text += "Communication Successful..." + "(time:" + DateTime.Now.ToString("hh:mm:ss") + ")\r\n\r\n";
                            Infor_Text.ScrollToEnd();
                        })
                            );

                        if (Str.Contains("Over"))
                        {
                            KillALLProcess("", "iperf"); KillALLProcess("", "cmd");
                        }
                    }
                    else if (mode.Contains("Client"))
                    {
                        Communication_Send("OK to client", Pre_IP, 11111);
                        using (Process p_client = new Process())
                        {
                            p_client.StartInfo.FileName = "cmd.exe";
                            //p_client.StartInfo.WorkingDirectory = "..\\Debug\\iperf2\\";
                            p_client.StartInfo.UseShellExecute        = false; //是否使用操作系统shell启动
                            p_client.StartInfo.RedirectStandardInput  = true;  //接受来自调用程序的输入信息
                            p_client.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
                            p_client.StartInfo.RedirectStandardError  = true;  //重定向标准错误输出
                            p_client.StartInfo.CreateNoWindow         = true;  //不显示程序窗口
                            p_client.Start();                                  //启动程序

                            //improve
                            p_client.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
                            p_client.BeginOutputReadLine();

                            p_client.StandardInput.WriteLine(Str);
                            int           exit_cnt   = 2;
                            string        output     = "";
                            List <string> ReturnStr  = new List <string>();
                            bool          IsRunAgain = false;
                            while (true)
                            {
                                ReadLine_str = "";
                                try
                                {
                                    string str = Communication_Receive(11111);
                                    if (str == "stop")
                                    {
                                        IsRunAgain = true;
                                        break;
                                    }
                                }
                                catch
                                {
                                }
                                output = ReadLine_str;
                                //string str = p_server.StandardOutput.ReadLine();
                                if ((output != "") && (output != null))
                                {
                                    string[] sArray = output.Split('&');
                                    foreach (string str in sArray)
                                    {
                                        ReturnStr.Add(str);
                                        Communication_Send(str.Replace("&", ""), Pre_IP, 11111);
                                    }
                                }
                                else if (output == "")
                                {
                                    exit_cnt--;
                                    if (exit_cnt == 0)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (IsRunAgain == true)
                            {
                                continue;
                            }
                            Thread.Sleep(100);
                            Communication_Send("Over", Pre_IP, 11111);
                            Thread.Sleep(100);
                            p_client.WaitForExit();//等待程序执行完退出进程
                            //p_client.Kill();
                            p_client.Close();
                            KillALLProcess("", "iperf"); KillALLProcess("", "cmd");
                            //p_client.CloseMainWindow();
                            int i = 1;
                            while (i != 0)
                            {
                                ReturnStr.Add("Measurement Over");
                                i--;
                            }
                            foreach (string str in ReturnStr)
                            {
                                Communication_Send(str, Pre_IP, 11111);
                                Thread.Sleep(30);
                            }
                            this.Dispatcher.Invoke(
                                new Action(
                                    delegate
                            {
                                Infor_Text.Text += "Communication Successful..." + "(time:" + DateTime.Now.ToString("hh:mm:ss") + ")\r\n\r\n";
                                Infor_Text.ScrollToEnd();
                            })
                                );
                        }
                    }
                }
                Thread.Sleep(10);
            }
        }