Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use my public IP with HttpListener

I've got this so far...

public static HttpListener listener = new HttpListener();
public static string startUpPath = Application.StartupPath;
public  WebServer()
{
    listener.Start();

    listener.Prefixes.Add("http://(here I want my public ip)/");
    Thread t = new Thread(new ThreadStart(clientListener));
    t.Start();
}

But when I initialize the class it says "The specified Network format is not valid"

The translation may not be perfect because my visual studio language is in Spanish. My ip looks like 95.^^.^^^.^^ and I think that may be the problem because it works when I use my local ip.

//Edited

The exact exception is: "El formato del nombre de red especificado no es válido" Which is"The specified network name is not valid". If I add the prefix "http:// + :80/" it still going, but how can I access that through my public ip?

like image 385
Louis Castro Avatar asked Jul 23 '13 15:07

Louis Castro


2 Answers

When setting this up you should use your internal IP, since that is the actual IP you are listening on. In-order to get traffic from the your external IP, you need to forward that traffic from your router to your computer that is listening on its internal IP.

One way to set it up is to use port forwarding, from your router, you would want to direct any traffic that comes in on port 80 to your computer.

More info: http://en.wikipedia.org/wiki/Port_forwarding

like image 186
Jamie Redman Avatar answered Nov 14 '22 21:11

Jamie Redman


As Jamie said, your HTTPListener should be bound to the IP address on the network card where the application is running. Unless you have a NIC that is actually configured with a public IP (e.g. not going through a router) then your private address is the one to use.

An easy way to check this is to run IPCONFIG and see what IP address is listed.

like image 30
GMAP Avatar answered Nov 14 '22 22:11

GMAP