这让我很伤心。我有一个运行微型 Web 服务器的 httplistener,基于这个项目:https://gist.github.com/define-private-public/d05bc52dd0bed1c4699d49e2737e80e7?permalink_comment_id=3636920
问题是这样的:我已经将程序设置为在计算机的公共 ip 上侦听,在验证您已经以管理员权限启动它之后。不过,我仍然无法从另一台设备访问服务器,这就是我首先试图通过询问管理员来解决的问题。
这是我的全部代码,包括验证端口可用的代码,如果不可用则选择另一个端口。所有这些都工作正常,但我根本无法远程调用 Web 服务器,即使我指定了端口。
TryBindListenerOnFreePort(out listener, out url);
Console.WriteLine("Listening for connections on {0}", url);
和做繁重工作的功能:
//source: https://stackoverflow.com/a/46666370/9008140
public static bool TryBindListenerOnFreePort(out HttpListener httpListener, out string url)
{
// IANA suggested range for dynamic or private ports
const int MinPort = 80;
const int MaxPort = 65535;
url = "";
for (int port = MinPort; port < MaxPort; port++)
{
if (port == 81)
{
port = 8000;
}
httpListener = new HttpListener();
if (GetIsElevated())
{
url = "http://" + Connection.GetLocalIP() + ":";
}
else
{
url = "http://localhost:";
}
url += $"{port}";
httpListener.Prefixes.Add(url + "/");
try
{
httpListener.Start();
return true;
}
catch(Exception ex)
{
// nothing to do here -- the listener disposes itself when Start throws
}
}
httpListener = null;
return false;
}
回调例程没有什么特别之处。它返回一些按钮供用户单击。重要的是它不能在同一子网上的远程系统上运行。注意:我完全关闭了防火墙。没关系。
我会接受关于为什么会发生这种情况以及该怎么做的信息,或者我想要的不同的免费库或 Nuget 包,只要它不是大到无法忍受,因为我在 zip 文件下载中分发我的应用程序。