使用System.Net.HttpListener时是否有一种方法可以启用SO_REUSEADDR套接字选项? [英] Is there a way to enable the SO_REUSEADDR socket option when using System.Net.HttpListener?

查看:128
本文介绍了使用System.Net.HttpListener时是否有一种方法可以启用SO_REUSEADDR套接字选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在已经搜索了一个多小时,并且在文档或网络示例中都找不到如何启用 SO_REUSEADDR 选项的方法, code> HttpListener 类。

Been searching for this for over an hour now, and I have not found in documentation nor in examples on the web how to enable the SO_REUSEADDR option when working with the HttpListener class.

我的特定用例在Powershell脚本中,但是即使C#中有答案,那就好了。

My specific use case is within a Powershell script, but even if there is an answer in C#, that would be fine. This is about as cut-and-dry as it gets:

$Listener = New-Object System.Net.HttpListener
$Listener.Prefixes.Add('http://+:9999/')
Try
{
    $Listener.Start()
    # While loop here to handle requests; omitted for brevity
}
Catch
{
    # omitted here for brevity
}

有时候,我 Ctrl-C 在测试时跳出脚本,并以这种方式离开循环 $ Listener.Stop()被调用,套接字被正确关闭。 (是的,我知道-如果事情会中断,请不要这样做)

There are times where I Ctrl-C to break out of the script while testing, and leaving the loop this way does not allow $Listener.Stop() to be called and the socket properly closed. (Yes, I know - don't do this if it breaks things)

通常,当我需要监听套接字时,我会使用 SO_REUSEADDR 处理无法正常清理的情况,以使我可以重新启动侦听器,而不必等待30分钟使端口再次可用。有没有办法用 HttpListener 做到这一点,而我只是想念它?

Normally, when I need a listening socket, I make use of SO_REUSEADDR to handle those times where things don't clean up properly to allow me to restart my listener without having to wait 30 minutes for the port to be available again. Is there a way to do this with HttpListener and I'm just missing it?

推荐答案

否,您不能将 SO_REUSEADDR HttpListener 一起使用。

No, you can't use SO_REUSEADDR with HttpListener.

原因是 HttpListener 并不实际管理底层套接字-它只是向注册应用程序级前缀HTTP.sys 驱动程序,该驱动程序又管理基础连接。

The reason is that the HttpListener doesn't actually manage the underlying socket - it simply registers application-level prefixes with the HTTP.sys driver, who in turn manages the underlying connection.

要使用 SO_REUSEADDR ,则可以创建 TcpListener 并使用<$设置基础套接字上的套接字选项(可通过 Server 属性访问) c $ c> SetSocketOption():

To use SO_REUSEADDR, you could create a TcpListener and set the socket option on the underlying socket (accessible through the Server property) with SetSocketOption():

$TcpListener = New-Object System.Net.Sockets.TcpListener 9999
$TcpListener.Server.SetSocketOption("Socket", "ReuseAddress", 1)
$TcpListener.Start()

但是现在您必须实现 HttpListener 自己提供的所有便捷功能(标题

But now you have to implement all the convenient things that HttpListener provides on you own (header parsing, content validation etc.).

话虽如此,这可能会解决您面临的实际问题:

That being said, this may solve the practical issue you face:

try{
    $Listener.Start()
}
catch{
}
finally{
    $Listener.Stop()
    $Listener.Dispose()
}

(不要将其粘贴到您的提示/ ISE中,您需要将其保存到脚本中,或最后放入 在中断时进行正确评估)

(don't paste this into you prompt/ISE, you need to save this to a script, or enclose in a script block for finally to be evaluated properly on interruption)

这篇关于使用System.Net.HttpListener时是否有一种方法可以启用SO_REUSEADDR套接字选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆