Tuesday, 30 October 2012

Checking If the Connection can be made to the Server or Not

Checking If the Connection can be made to the Server or Not

Hi all, Today I will give you a code snippet to find out if a connection can be made to server or not provided that if you know the Server Name and Port Number..

This can be useful in scenarios when you can switch to other server based upon the fact the connection to a particular server failed and still u want to keep ur website alive..

Here is the Code Snippet to achieve this:


public bool ConnectionExist(string Server, int Port)

        {

            {

                try

                {

                    TcpClient clnt = new TcpClient(Server, Port);

                    clnt.Close();

                    return true;

               }

                catch (System.Exception ex)

                {

                    return false;

                }

            }

        }

No comments:

Post a comment

Thank You for Your Comments. We will get back to you soon.

back to top