c# - 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))。我希望你能帮助我!祝你今天过得愉快!
解决方案
推荐阅读
- mysql - 使用计数、总和的复杂 SQL 查询
- algorithm - 我需要一个排序算法
- c++ - 如何修复不会在退出选择时终止的 do-while 循环
- flutter - Flutter Doctor 让 Flutter 升级抱怨本地变化
- typescript - Typescript 中的同步和异步代码
- javascript - Node.js addEventListener 和其他没有浏览器的事件
- bash - urxvt 中的 bash - 读取变量时在 alrady 键入的字符上退格
- python - 为什么这个 Kotlin 代码的行为与这个 Python 代码不同?
- c++ - 不理解此递归函数中的返回 (C++)
- ios - 为什么 SCNLights 适用于 Apple 的内置几何图形,而不适用于 OBJ 或 DAE 文件?