c#webserver工作在本地(127.0.0.1)但没有公开

时间:2015-06-23 14:59:54

标签: c# web ip server

I used this web server for c#: 我也尝试了其他项目,但我无法连接到我的IP。

这是主要功能:

/// <param name="path">Directory path to serve.</param>
public SimpleHTTPServer(string path)
{
    //get an empty port
    TcpListener l = new TcpListener(IPAddress.Loopback, 0);
    l.Start();
    int port = ((IPEndPoint)l.LocalEndpoint).Port;
    l.Stop();
    this.Initialize(path, port);
}

例如:

127.0.0.1 - working
localhost - working
122.211.2.27 - not working

1 个答案:

答案 0 :(得分:0)

问题可能与您明确创建绑定到环回的TcpListener的事实有关:

TcpListener l = new TcpListener(IPAddress.Loopback, 0);

阅读documentation,了解有关如何在没有显式绑定的情况下创建侦听器或绑定到其他IP地址的更多详细信息。