开发者

C#/.NET: FInd out whether a server exists, Query DNS for SVC records

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 otherwis开发者_开发知识库e 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?


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


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜