0

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.

Jason Aller
  • 3,391
  • 28
  • 37
  • 36
Paul Rusu
  • 233
  • 1
  • 4
  • 17

2 Answers2

1

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

Peter Ritchie
  • 33,368
  • 9
  • 74
  • 96
  • Skype or a local web server are the main culprits for this – Andrew Brock May 16 '12 at 12:37
  • 1
    You don't understand. Yes, there is another application probably IIS. But Fiddler works without any problem. How can I do it too? – Paul Rusu May 16 '12 at 12:39
  • @PaulRusu I believe that fiddler works as an extension for the browser. If you only want it to work with 1 browser, this is fine, otherwise you will have to re-write parts of your code several times for each browser. The alternative is to sniff for packets on the network with WinPCAP, but this wont work for any website using https:// – Andrew Brock May 16 '12 at 12:42
  • @AndrewBrock pretty sure fiddler isn't a browser extension *as such* - it may have some hooks (in particular for SSL), but primarily it is lower level than that – Marc Gravell May 16 '12 at 12:50
1

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

BrokenGlass
  • 149,257
  • 27
  • 271
  • 318
  • You are right. I wrote code for incoming connections. I want to write code for outgoing connections like Fiddler. Can you give me a clue how to do that? – Paul Rusu May 16 '12 at 12:43
  • There's some good info on how to create a proxy in this SO thread which should get you started: http://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c – BrokenGlass May 16 '12 at 12:46