none
C# 2.0 - How to check that server is alive

    Question

  • Hi

    1. I wanna check that whether my DB server is alive using IP Address of the server.
    2. If my DB server is alive then I wanna check whether SQL Server Service is running or not.

    Currently I m using this code to check connection but it takes almost 15 to 20 sec to generate exception. But I have only 7 sec between two DB request. So my data is over writing.

     if (m_sqlConn.State == ConnectionState.Closed)
                        m_sqlConn.Open();

    Thanks in advance

    Regards
    Adeel Arshad
    Regards,Adeel Arshad
    Saturday, May 23, 2009 5:24 AM

Answers

  • Take a look at the Ping class.  Its in the System.Net.NetworkInformation name space.  You can ping a server and get a reply to determine if you can connect to it.  There is a PingReply function that returns the status and connectivity of the server address being pinged.  There are eight different PingReply methods, each one uses different parameters.

    IMO, the best PingReply function to use is the one where you send the address as a set of address bytes.  The reason is if there is no network connection and you try to send the server string address then there is no way to reconcile that address, so you have an expensive exception to catch.  But if you send a properly formed set of address bytes, it will simply time out, and in general, no exception will be thrown.

    Here's a  quick example of pinging a server using the Ping class.  This method attempts to ping a server by sending it 4 bytes of data, and times out after 1.5 seconds if it can't connect by that time.

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

     

    • Edited by Matt Fomich Monday, May 25, 2009 9:01 AM grammer
    • Marked as answer by khushi83 Monday, May 25, 2009 1:48 PM
    Monday, May 25, 2009 8:59 AM

All replies


  • You can use WMI for querying remote service information.



    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    Saturday, May 23, 2009 10:54 AM
  • Take a look at the Ping class.  Its in the System.Net.NetworkInformation name space.  You can ping a server and get a reply to determine if you can connect to it.  There is a PingReply function that returns the status and connectivity of the server address being pinged.  There are eight different PingReply methods, each one uses different parameters.

    IMO, the best PingReply function to use is the one where you send the address as a set of address bytes.  The reason is if there is no network connection and you try to send the server string address then there is no way to reconcile that address, so you have an expensive exception to catch.  But if you send a properly formed set of address bytes, it will simply time out, and in general, no exception will be thrown.

    Here's a  quick example of pinging a server using the Ping class.  This method attempts to ping a server by sending it 4 bytes of data, and times out after 1.5 seconds if it can't connect by that time.

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

     

    • Edited by Matt Fomich Monday, May 25, 2009 9:01 AM grammer
    • Marked as answer by khushi83 Monday, May 25, 2009 1:48 PM
    Monday, May 25, 2009 8:59 AM
  • Take a look at the Ping class.  Its in the System.Net.NetworkInformation name space.  You can ping a server and get a reply to determine if you can connect to it.  There is a PingReply function that returns the status and connectivity of the server address being pinged.  There are eight different PingReply methods, each one uses different parameters.

    IMO, the best PingReply function to use is the one where you send the address as a set of address bytes.  The reason is if there is no network connection and you try to send the server string address then there is no way to reconcile that address, so you have an expensive exception to catch.  But if you send a properly formed set of address bytes, it will simply time out, and in general, no exception will be thrown.

    Here's a  quick example of pinging a server using the Ping class.

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

     

    Matt,

    The answer on the subject is correct but problem behind the subject not, it is about a DB server on a running known connected, where the problem is how to see if the DB server is running beside checking the connection.
    I think that is a difficult question to answer.

    :-)

    Cor
    Monday, May 25, 2009 9:03 AM
  • My bad--I did not read the last part of the question.

    Easiest way to detect a process would prob. be to make a web page and install it on the server, and have the web page check for the service and start it as needed.  You will probably need the admin person for the server tweak the CAS settings to allow this web page to run--I have never tried this on a remote server, but I am sure it would probably be blocked from executing by default.

    Anyway, the easiest way to verify any process is running is to use the Process class to return an array of all instances of a specific process.  Then check that the array has at least one element, at least one instance running, and start the process if it is not running.

    Here's an example that runs properly on the local host/local PC.  I have this set to check that Notepad is running, and it starts notepad if it is not running.  Of course Notepad is different than a Windows service, but they are both processes that run and they can be started and stopped.

    This is done in ASP.Net / C# , version 2005/ .Net Framework 2.0.

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Web Service Checker</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBoxStatus" runat="server" Height="120px" Style="z-index: 100;
                left: 128px; position: absolute; top: 104px" TextMode="MultiLine" Width="552px"></asp:TextBox>
            <asp:Label ID="CaptionLabel" runat="server" BorderStyle="None" Height="56px" Style="font-size: large;
                z-index: 103; left: 176px; font-family: arial; position: absolute; top: 32px"
                Text="Verify Web Service Home Page." Width="448px"></asp:Label>
            <asp:Button ID="CheckButton" runat="server" OnClick="CheckButton_Click" Style="z-index: 102;
                left: 568px; position: absolute; top: 248px" Text="Check Status" />
        
        </div>
        </form>
    </body>
    </html>
    

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Check first time this page loads.
            if (!IsPostBack)
            {
                VerifyProcess();
            }
        }
    
        private void VerifyProcess()
        {
        string ProcName = "Assembly_Name_Of_Service_Here";
            // For example, Notepad assembly name used.
            ProcName = "Notepad";
            System.Diagnostics.Process[] ProcArry = System.Diagnostics.Process.GetProcessesByName(ProcName);
    
            if (ProcArry == null || ProcArry.Length == 0)
            {
                // Not running so start it.
                System.Diagnostics.Process.Start(ProcName);
                // Assumes a textbox that has textmode
                // set to multiline (scrollbar visible).
                TextBoxStatus.Text = "process was not running, but start " +
                    "command has been issued. Press the Status button again " +
                    "in a few seconds to verify it is running properly.";
            }
            else
            {
                TextBoxStatus.Text = "Process is running properly.";
            }
    
        }
    
        protected void CheckButton_Click(object sender, EventArgs e)
        {
            VerifyProcess();
        }
    }
    

    The web page has one label, one textbox, and one button.  The button checks and starts the service as needed.


    Hope this helps.
    Tuesday, May 26, 2009 1:13 AM