C#/.NET:查找服务器是否存在,查询 DNS 中的 SVC 记录

发布于 2024-08-29 08:55:54 字数 424 浏览 1 评论 0 原文

编写一个 cleint/服务器工具,我的任务是尝试找到要连接的服务器。我很乐意让用户的事情尽可能简单。因此,我的想法是:

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

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

任何人都知道如何:

  • 以最快的方式找出服务器是否存在并回答(即在线)?如果服务器不存在,TCP 可能需要很长时间。对我来说,UDP 风格的 ping 听起来是个好主意。 PING 本身可能不可用。
  • 任何人都知道如何从 .NET 内部请求特定(默认)域中的 SVC 记录?

Writing a cleint/Server tool I am tasked trying to find a server to connect to. I would love to make things as easy as possible for the user. As such, my idea is to:

  • CHeck whether specific servers (coded by name) exist (like "mail.xxx" for a mail server, for example - my exampüle is not a mail server;)
  • Query otherwise for DNS SVC records, allowing the admin to configure a server location for a specific serivce (that the client connects to).

The result is that the user may have to enter only a domain name, possibly even not even that (using the registered standard domain of the computer in a LAN environment).

Anyone ideas how:

  • To find out whether a server exists and answers (i.e. is online) in the fastest way? TCP can take a long time if the server is not there. A UDP style ping sounds like a good idea to me. PING itself may be unavailable.
  • Anyonw knows how to ask from withint .NET best for a SVC record in a specific (the default) domain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间☆小暴躁 2024-09-05 08:55:54

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

来自此处

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;
    }
}

You can use ping from .NET but it needs the ip address of the server.

From 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;
    }
}

EDIT: How about this:

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;
    }
}
风尘浪孓 2024-09-05 08:55:54

对于你的问题,我会推荐一个完全不同的解决方案:

  1. 在服务器上在某个自定义端口上实现一个 icmp 服务器,你可以在其中接受一些“你在吗?”消息,并回答“是的,我是”;
  2. 在客户端,您向特定端口广播“您在吗”消息。
    这样您的客户端将始终知道哪些是可用的服务器。它将运行得更快,并且无需安装或配置任何 dns 服务。

To your problem I would recommend a completely different solution:

  1. on the server implement an icmp server on some custom port, where you accept some "are you there?" message, and answer with "yes, i am";
  2. on client side, you broadcast a "are you there" message to your specific port.
    This way your client will always know which are the available servers. It will work much faster, and without any dns service installed or configured.
雨落星ぅ辰 2024-09-05 08:55:54

测试服务是否存在且处于活动状态的最佳方法可能是使用该服务的本机协议。否则我会选择 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);

The best way to test if a service exists and is alive is probably using the native protocol of that service. Otherwise I would go with ping (ICMP ECHO).

As for looking up the SRV-record (I assume you mean "SRV" sinces there is no "SVC" type), you could use the "DNS Client Library for .NET" (http://www.simpledns.com/dns-client-lib.aspx) - for example:

var Response = JHSoftware.DnsClient.Lookup("_ftp._tcp.xyz.com", 
                                       JHSoftware.DnsClient.RecordType.SRV);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文