2

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.

Pankaj
  • 9,749
  • 32
  • 139
  • 283
Anirudha
  • 32,393
  • 7
  • 68
  • 89

3 Answers3

5

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 Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • @Anirudha Could you show your code for that? You need to add two separate prefixes, one for localhost (if you want to listen to that) and one for 182.X.X.X. You can only add addresses actually assigned to your computer, for example if your computer has a 192.x.x.x address and is behind a port forwarding proxy with an ip of 182.x.x.x, you can't add the 182.x.x.x address as a prefix to listen to. – Joachim Isaksson Feb 25 '12 at 17:12
  • 1
    @Anirudha Another hint, you can add a prefix of `http://+:80` to listen to port 80 of all addresses assigned to your computer at once. – Joachim Isaksson Feb 25 '12 at 17:16
  • actually i used h**p://182.X.X.X/localhost/ instead of h**p://localhost/ but still no fruit! – Anirudha Feb 25 '12 at 17:29
  • 1
    @Anirudha You should use only `http://182.x.x.x:80`, no localhost. Alternately, use `http://+:80` to automatically use any IP address the machine has. – Joachim Isaksson Feb 25 '12 at 18:03
1

@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
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • i am having brodband of a private company..Is that ip-address of proxy server! – Anirudha Feb 25 '12 at 19:48
  • 1
    You need a IP-address which can be accessible to others (exposed publicly). So that you can host your app there.. – Pankaj Feb 25 '12 at 19:56
0

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..

Anirudha
  • 32,393
  • 7
  • 68
  • 89