【问题标题】:How to check remote IP and Port is available?如何检查远程IP和端口是否可用?
【发布时间】:2014-05-19 04:34:59
【问题描述】:

我必须检查远程 IP 和端口是否可用。如果可用,它将移动到下一个表单。如果不可用,它应该进入初始状态。我尝试使用这个

while (true)
{
    IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
    IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
    -------
    -------
    -------
}

我正在展示示例编码。它正在检查本地 IP 和端口并移动到下一个表单。它将检查本地端口和 IP 是否可用。如果端口和 IP 不可用,它将进入初始阶段并且它正在工作很好。我必须检查远程端口和 IP。

【问题讨论】:

    标签: c# .net winforms networking tcp


    【解决方案1】:

    使用.NET 的Ping 类来检查系统是否已启动并连接,使用PortScanner 来检查端口是否打开。检查这些链接以进一步阅读和探索。

    http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping%28v=vs.110%29.aspx

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/8e4410bd-307f-4264-9575-cd9882653945/help-with-portscanner-in-c?forum=csharpgeneral

    public static bool PingHost(string hostUri, int portNumber)
    {
        try
        {
            using (var client = new TcpClient(hostUri, portNumber))
                return true;
        }
        catch (SocketException ex)
        {
            MessageBox.Show("Error pinging host:'" + hostUri + ":" + portNumber.ToString() + "'");
            return false;
        }
    }
    

    【讨论】:

    • 谢谢,这正是我想要的!只有一个想法:TcpClient 实现了IDisposable,所以你可以这样做:using (TcpClient client = new TcpClient(_adress, port)) { return true; }
    • 谢谢,这正是我想要的!
    猜你喜欢
    • 2014-12-04
    • 2010-09-12
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多