Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Test for local host

Posted on 2013-05-31
4
Medium Priority
?
574 Views
Last Modified: 2013-06-01
How can I detect if the local host is running?

example:

if (local host)
     {
      Do something
     }
else  //remote host
     {
      Do Nothing
     }

Thanks
0
Comment
Question by:Dovberman
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Jitendra Patil
ID: 39212493
try the code given below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
                Console.WriteLine("Local IP Address: " + GetIPAddress(Dns.GetHostName()));
                Console.WriteLine("Google IP:" + GetIPAddress("google.com"));

                if (GetIPAddress(Dns.GetHostName()).ToString() == "::1")
                {
                    Console.WriteLine("This is local host address");
                }
                else
                {
                    Console.WriteLine("This is website address");
                }

                Console.ReadLine();
        }

        public static IPAddress GetIPAddress(string hostName)
        {
            Ping ping = new Ping();
            var replay = ping.Send(hostName);

            if (replay.Status == IPStatus.Success)
            {
                return replay.Address;
            }
            return null;
        }

    }
}

Open in new window


hope this helps.
0
 
LVL 16

Accepted Solution

by:
Rose Babu earned 1200 total points
ID: 39212545
Try this...
HttpContext context = HttpContext.Current;

if (context.Request.Url.ToString().StartsWith("http://localhost"))
{
	// running from Local Host

}
else
{
	// running from Remote Host

}

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39212788
I used the suggestion from srosebabu.

Thanks to both of you.
0
 

Author Closing Comment

by:Dovberman
ID: 39212790
Thank you
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question