I am attempting to create a windows application that listens to port 80. I started with a socket, then tried a TCP, and eventually an HTTP, Listener - nothing works. I keep getting an access denied error.

As it turns out, the geniuses at Microsoft flow all port 80 traffic through the Http.sys DLL - which means that only one process is allowed to listen on that port ever. In my case, IIS is running (this is my DEV box) and that means that I either need to force IIS to share port 80 or I need to dynamically re-route port 80 traffic through another port.

Does anybody know how to do either of those tasks? The re-routing would ideally occur through code inside my Windows application.

The last option is to change port 80 in the default website to another port - but that broke the Visual Studio debugger and I also do not know how to crrect that issue.

Any of the above solutions would be awesome - thanks!

I have posted a page that explains how to move IIS to another port and hook up Visual studio http://www.ultimateproxylist.com/ChangingIIS.aspx

The problem I still have - even after moving IIS off of Port 80 - is that no listener is actually working. I even grabbed the following code sample directly off the MSDN site and even IT does not work! I am on a Windows 7 machine - any ideas?!?

if (!HttpListener.IsSupported) 
            { 
                Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); 
                return; 
            } 
// Create a listener. 
            HttpListener listener = new HttpListener(); 
listener.Prefixes.Add("http://*:80/"); 
            listener.Start(); 
            Console.WriteLine("Listening..."); 
            // Note: The GetContext method blocks while waiting for a request.  
            HttpListenerContext context = listener.GetContext(); 
            HttpListenerRequest request = context.Request; 
            // Obtain a response object. 
            HttpListenerResponse response = context.Response; 
            // Construct a response. 
            string responseString = "<HTML><BODY> Hello world!</BODY></HTML>"; 
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); 
            // Get a response stream and write the response to it. 
            response.ContentLength64 = buffer.Length; 
            System.IO.Stream output = response.OutputStream; 
            output.Write(buffer, 0, buffer.Length); 
            // You must close the output stream. 
            output.Close(); 
            listener.Stop();

Port 80 is the default port used by HTTP servers. only 1 program at a time can be the daemon of a local port. that's basic sockets 101. And by default on many XP installations there is a web server running. you might consider using a different port.

unless you are writing a web server, in which case you just need to find out which process is listening on port 80 and kill it.

I figured that part out - the problem now, I think, isthat all of the listeners, socket classes, etc... are built to handle traffic directed to them. For example, it is relatively easy to build a TCP Server and Client and have the Clinet sned traffic that a TCP Listener on the Server handles.

The problem I have is that I want to listen to traffic going from local browsers out to the world. Even though the listener is running on Port 80, on a local IP address, the BeginAccept method never fires and I do not know how to correct this issue...

Ah, you don't want to create an end point, you want to watch the traffic. or a packetsniffer! for this you need to manually create a socket instead of using a tcpListner or httpListner subclass. and you need to do a rawbind giving you access to the port without taking ownership of it.

this example claims to be able to do it http://www.c-sharpcorner.com/uploadfile/leonidmolochniy/simplesnifferincs11222005232804pm/simplesnifferincs.aspxe Although there are errors in the code, the makefile doesn't work and and there is an error when it creates a dispose method by using an override keyword which shouldn't be there. Other than that it compiles but it has other errors. But the concept is there and you might be able to use it to do what you are looking for.

here is an explanation of how to create a RawSocket that doesn't block the data just allows you to look at it. http://forums.techpowerup.com/showthread.php?t=61792

an here is a codeproject example of a network sniffer http://www.codeproject.com/KB/IP/CSNetworkSniffer.aspx

Thanks for the reply - I have already delved into Raw Sockets and I have written some classes to parse out IP, TCP, UDP and DNS header information. I am still having some issues but I think I am on the correct path.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.