创建一个windows应用程序来检查与几个服务器的连接。如何检查是否存在与远程ip上指定端口的连接?
是否有内置的窗口命令来检查远程端口(命令行)?
在检查之前,不知道端口是否是TCP或UDP端口。这是怎么可能的。
提前感谢
发布于 2014-10-09 11:51:38
我的(德语)博客:在这里输入链接描述
更新:这个工具现在也支持UDP和多端口平。
如果端口打开,它将返回true。这是老式的代码(没有TPL),但它能工作。
var result = false;
using (var client = new TcpClient())
{
try
{
client.ReceiveTimeout = timeout * 1000;
client.SendTimeout = timeout * 1000;
var asyncResult = client.BeginConnect(host, port, null, null);
var waitHandle = asyncResult.AsyncWaitHandle;
try
{
if (!asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(timeout), false))
{
// wait handle didn't came back in time
client.Close();
}
else
{
// The result was positiv
result = client.Connected;
}
// ensure the ending-call
client.EndConnect(asyncResult);
}
finally
{
// Ensure to close the wait handle.
waitHandle.Close();
}
}
catch
{
}
}
return result;https://stackoverflow.com/questions/26277390
复制相似问题