代码之家  ›  专栏  ›  技术社区  ›  TomTom

C/.NET:查找服务器是否存在,查询DNS以获取SVC记录

  •  3
  • TomTom  · 技术社区  · 14 年前

    编写一个cleint/server工具,我的任务是试图找到一个连接到的服务器。我很乐意为用户提供尽可能简单的服务。因此,我的想法是:

    • 检查是否存在特定的服务器(按名称编码)(例如,邮件服务器的“mail.xxx”-我的示例不是邮件服务器;)
    • 否则查询dns svc记录,允许管理员为特定服务(客户端连接到的)配置服务器位置。

    结果是,用户可能只需要输入一个域名,甚至可能不需要输入(在局域网环境中使用计算机的注册标准域)。

    任何人都知道如何:

    • 找出服务器是否存在并以最快的方式回答(即是否在线)?如果服务器不在,TCP可能需要很长时间。udp风格的ping对我来说是个好主意。ping本身可能不可用。
    • Anyonw知道如何向WithInt.NET询问特定(默认)域中SVC记录的最佳值?
    3 回复  |  直到 13 年前
        1
  •  1
  •   Jesper    14 年前

    测试一个服务是否存在并存在的最佳方法可能是使用该服务的本机协议。否则我将使用ping(icmp echo)。

    至于查找srv记录(我假设您的意思是“srv”,因为没有“svc”类型),您可以使用“dns client library for.net”( http://www.simpledns.com/dns-client-lib.aspx )-例如:

    var Response = JHSoftware.DnsClient.Lookup("_ftp._tcp.xyz.com", 
                                           JHSoftware.DnsClient.RecordType.SRV);
    
        2
  •  4
  •   James Skemp    13 年前

    您可以使用来自.NET的ping,但它需要服务器的IP地址。

    here :

    internal bool PingServer()
    {
        bool netOK = false;
        // 164.110.12.144 is current server address for server: nwhqsesan02
    
        byte[] AddrBytes = new byte[] { 164, 110, 12, 144 }; // byte array for server address.
        using (System.Net.NetworkInformation.Ping png = new System.Net.NetworkInformation.Ping())
        {
            System.Net.IPAddress addr;
            // Sending ping to a numeric byte address has the best change of 
            // never causing en exception, whether network connected or not.
            addr = new System.Net.IPAddress(AddrBytes);
            try
            {
                netOK = (png.Send(addr, 1500, new byte[] { 0, 1, 2, 3 }).Status == IPStatus.Success);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString()); 
                netOK = false;
            }
            return netOK;
        }
    }
    

    编辑:这个怎么样:

    bool ConnectionExists()
    {
        try
        {
            System.Net.Sockets.TcpClient clnt=new System.Net.Sockets.TcpClient("www.google.com",80);
            clnt.Close();
            return true;
        }
        catch(System.Exception ex)
        {
            return false;
        }
    }
    
        3
  •  2
  •   jfs    13 年前

    对于您的问题,我建议您采用完全不同的解决方案:

    1. 在服务器上的某个自定义端口上实现一个icmp服务器,在该端口上接受一些“您在吗?”留言,回答“是的,我是”;
    2. 在客户端,您向特定端口广播一条“Areyouthere”消息。 这样,您的客户机将始终知道哪些是可用的服务器。它将工作得更快,而且没有安装或配置任何dns服务。