Question

I want to create a website with URL as my IP-address[ex: 192.X.X.X]

That website would respond with a "HELLO THERE" message to any user who accesses my URL.

I use the following code to do this![its just a basic code with no threading]

class listenToHTTP
{
    HttpListener _listner;
    public void start()
    {
        _listner = new HttpListener();
        _listner.Prefixes.Add("http://localhost/");//default port 80
        _listner.Start();
    }
    public void process()
    {
         while (true)
         {
            HttpListenerContext context = _listner.GetContext();
            byte[] output = Encoding.ASCII.GetBytes("HELLO THERE");
            context.Response.ContentEncoding = Encoding.ASCII;
            context.Response.ContentLength64 = output.Length;
            context.Response.OutputStream.Write(output, 0, output.Length);

          }
     }
}

The problem is that i dont know the IP-address through which anyone would access.

It perfectly shows the response "HELLO THERE" when I use http://localhost/ as URL.

But what IP-address would other people use so that they can access my simple website.

I have tried my IP-address in the browser but it doesnt work.

Was it helpful?

Solution 3

The problem was that I was referring to a private network address which are local to the network and cannot be accessed by anyone outside that private network..

These are the range of ip-address that are used in private network and so systems with this address cannot be a server or host a website..

10.0.0.0 to 10.255.255.255

172.16.0.0 to 172.31.255.255

192.168.0.0 to 192.168.255.255

You should use a public address..

OTHER TIPS

There are two things to look out for when doing this;

  • If you listen to a localhost address, only localhost will be able to connect to your HttpListener. You'll need to add a prefix with http://192.X.X.X/ (where the 192.X.X.X is your local IP of course) and listen to that. That may (depending on your operating system) require you to run as admin, at least if you want to do it on a port < 1024. You can test if this works by connecting to your IP# from your local machine instead of a localhost address.

  • If you're running Windows, the firewall may get in the way. If it seems to (ie you can connect to your IP# from the local machine but nothing else can connect) you'll need to open the port manually. There are plenty of guides how to do this on Google.

@Joachim reply is already good enough. I will like to add a bit more...

  1. You need to expose the above mentioned IP address publicly to get the URL accessible to others.
  2. In case of Exposing the URL to your domain only (i.e. in case of Intranet), check with your System Administrator to configure the IP Address on Intranet.
  3. Localhost settings accessibility is confined to your machine only.
  4. Make sure to check the firewall constraint for the URL accessibility to implement Point 1 or 2

For more information check the reference for HTTPListener

  1. HTTPListener
  2. HTTPListener
  3. HTTPListener
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top