Paul Rusu
Paul Rusu

Reputation: 233

implement a 80 http port listener

I want to implement functionality similar to Fiddler.

I have tried 2 solutions:

HttpListener

HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:80/");
listener.Start();

When Start() is executed I get the error:

System.Net.HttpListenerException "The process cannot access the file because it is being used by another process"

which means that there is some other application listening on port 80 maybe IIS or whatever.

Sockets

IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 80);

// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);

 // Bind the socket to the local endpoint and listen for incoming connections.
 try
 {
     listener.Bind(localEndPoint);
// .....

When listener.Bind() is executed I get:

System.Net.Sockets.SocketException (0x80004005): Only one usage of each socket address (protocol/network address/port) is normally permitted

which means the same thing, there is already another application listening on port 80.

So how does Fiddler listen to port 80 without these problems? I would prefer to use a class like HttpListener because it's higher level instead of a lower level class like socket where I have to implement the HTTP Protocol.

Upvotes: 0

Views: 4658

Answers (2)

BrokenGlass
BrokenGlass

Reputation: 160852

So how does Fiddler listen to port 80 without these problems?

Fiddler is not accepting incoming connections on port 80 - it is acting as a proxy for outgoing HTTP connections. Some other application is using port 80 on your machine, so it is blocked for usage since there can only be one listener on a given port (in this case IIS as you mention in the comments).

Upvotes: 1

Peter Ritchie
Peter Ritchie

Reputation: 35881

That means something else is listening on port 80. You'll have to exit that application before yours will work.

Upvotes: 1

Related Questions