简体   繁体   English

实现一个 80 http 端口监听器

[英]implement a 80 http port listener

I want to implement functionality similar to Fiddler.我想实现类似于 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:执行Start()出现错误:

System.Net.HttpListenerException "The process cannot access the file because it is being used by another process" System.Net.HttpListenerException“进程无法访问该文件,因为它正被另一个进程使用”

which means that there is some other application listening on port 80 maybe IIS or whatever.这意味着还有一些其他应用程序在侦听端口 80,可能是 IIS 或其他什么。

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:listener.Bind()被执行时,我得到:

System.Net.Sockets.SocketException (0x80004005): Only one usage of each socket address (protocol/network address/port) is normally permitted System.Net.Sockets.SocketException (0x80004005):通常每个套接字地址(协议/网络地址/端口)只允许使用一次

which means the same thing, there is already another application listening on port 80.这意味着同样的事情,已经有另一个应用程序在监听端口 80。

So how does Fiddler listen to port 80 without these problems?那么 Fiddler 如何监听 80 端口而没有这些问题呢? 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.我更喜欢使用像 HttpListener 这样的类,因为它是更高级别的,而不是像套接字这样的低级别类,我必须在其中实现 HTTP 协议。

这意味着其他东西正在侦听端口 80。您必须先退出该应用程序,然后您的应用程序才能工作。

So how does Fiddler listen to port 80 without these problems?那么 Fiddler 如何监听 80 端口而没有这些问题呢?

Fiddler is not accepting incoming connections on port 80 - it is acting as a proxy for outgoing HTTP connections. Fiddler 不接受端口 80 上的传入连接 - 它充当传出HTTP 连接的代理。 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).一些其他应用程序正在您的机器上使用端口 80,因此它被阻止使用,因为给定端口上只能有一个侦听器(在本例中为您在评论中提到的 IIS)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM