WITH#/. NET: indicate if a server exists, Query DNS for SVC records

Writing a cleint / Server Tool I have been instructed to find a server to connect to. I would like to make everything as simple as possible for the user. Essentially my idea:

  • CHeck, are there specific servers (encoded by name) (for example, "mail.xxx" for a mail server, for example, my exam is not a mail server;)
  • A query for DNS SVC records that allows an administrator to configure the server location for a specific serivce (to which the client connects).

As a result, the user can enter only the domain name, perhaps even this (using the registered standard computer domain on the local network).

Any ideas like:

  • To find out if a server and answers exist (i.e. online) in the fastest way? TCP can take a long time if there is no server. UDP style ping sounds like a good idea to me. PING itself may not be available.
  • Does Anyonw know how to query with .NET best for writing SVC in a specific (default) domain?
+3
source share
3 answers

The best way to check if a service exists and if it exists is probably using its own protocol for the service. Otherwise, I would go with ping (ICMP ECHO).

SRV- ( , "SRV" sinces, "SVC" ), " DNS .NET" (http://www.simpledns.com/dns-client-lib.aspx) - :

var Response = JHSoftware.DnsClient.Lookup("_ftp._tcp.xyz.com", 
                                       JHSoftware.DnsClient.RecordType.SRV);
+1

ping .NET, 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;
    }
}

EDIT: :

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

:

  • icmp- - , " "? , ", ";
  • on the client side, you send the message "is there" to your specific port. Thus, your client will always know which of the available servers. It will run much faster and without any DNS service installed or configured.
+2
source

Source: https://habr.com/ru/post/1741301/


All Articles