tags:

views:

650

answers:

2

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?
A: 

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;

} }

Aseem Gautam
Does not work. PING needs the server allow ICMP - and that is "not something I can enforce at customer sites" (and yes, I can not ping half of the servery I connect to - stupid crazy thinking in the financial industry).
TomTom
Ah the finance industry, its not crazy, they have all our money. Check my update if that works.
Aseem Gautam
That does not tell me where MY OWN software servers are. This is not about "finding out whether I have internet", it is to allow my own software to try to guess it's own (installed) servers in a LAN scenario, so that people do not have to enter the server name. Similar to for example Exchange Auto-Config, or Office Communicator's way to find the OCS instance for itself.
TomTom
The end goal is that a user of my own software can start the client, and it will provide - asap - a list of found servers in the network.
TomTom
A: 

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