SignalR:无法连接到本地或任何其他 IP 地址

SignalR: can't connect to local or any other ip address

我正在尝试制作 SignalR 服务器和客户端架构,我可以在其中连接到“http://localhost:8080" or http://127.0.0.1:8080/ 但我无法连接我的本地 IP 地址,如“192. x.x.x" 那么可能是什么原因呢? 请帮助我,我也将我的代码放在这里...

 public partial class WinFormsServer : Form
        {
            private IDisposable SignalR { get; set; }
            const string ServerURI = "http://localhost:8080";

     private void ButtonStart_Click(object sender, EventArgs e)
            {
                WriteToConsole("Starting server...");
                ButtonStart.Enabled = false;
                Task.Run(() => StartServer());
            }
     private void StartServer()
            {
                try
                {
                    SignalR = WebApp.Start(ServerURI);
                }
                catch (TargetInvocationException)
                {
                    WriteToConsole("Server failed to start. A server is already running on " + ServerURI);
                    //Re-enable button to let user try to start server again
                    this.Invoke((Action)(() => ButtonStart.Enabled = true));
                    return;
                }
                this.Invoke((Action)(() => ButtonStop.Enabled = true));
                WriteToConsole("Server started at " + ServerURI);
            }
     class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.UseCors(CorsOptions.AllowAll);
                app.MapSignalR();
            }
        }

    }

要获得 local IP address,您可以使用此函数:

public static string GetLocalIPAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            throw new Exception("Local IP Address Not Found!");
        }

如果您想使用 FQDN - 完全合格的域名,那么您可以使用此功能:

public static string GetLocalFQDN()
        {
            var props = IPGlobalProperties .GetIPGlobalProperties();
            return props.HostName + (string.IsNullOrWhiteSpace(props.DomainName) ? "" : "." + props.DomainName);
        }

之后你可以使用:

SignalR = WebApp.Start("http://" + GetLocalFQDN() + ":8080");  

SignalR = WebApp.Start("http://" + GetLocalIPAddress() + ":8080");  

希望对您有所帮助。

因为你用的是this source我也用的一样
- 对于 FQDN,首先创建下面的函数。

public static string GetLocalFQDN()
                {
                    var props = IPGlobalProperties.GetIPGlobalProperties();
                    return props.HostName + (string.IsNullOrWhiteSpace(props.DomainName) ? "" : "." + props.DomainName);
                }

然后将const string ServerURI修改为:

string ServerURI =String.Concat("http://",GetLocalFQDN(),":8080");

-对于 LocalIPAdress,首先创建下面的函数,它将是您的本地地址:

public static string GetLocalIPAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            throw new Exception("Local IP Address Not Found!");
        }

并将 string ServerURI =String.Concat("http://",GetLocalFQDN(),":8080"); 更改为:

string ServerURI =String.Concat("http://",GetLocalIPAddress(),":8080");

希望对您有所帮助。

注意: 更改应在 WinFormsServer 项目的 WinFormsServer:Form class 中完成。

我尝试了不同的解决方案,但找不到正确的解决方案。

最后 我发现了仅与 权限 有关的问题。 运行 您的 SignalR 服务器应用程序 作为管理员。它将在本地 IP 上启动 运行,例如 192.168.X.X:9090,然后您的客户端应用程序可以使用此 IP 地址从任何其他 PC 连接此服务器。

class Program
{
    static void Main(string[] args)
    {
        var url = $"http://{GetLocalIPAddress()}:8080";
        using (WebApp.Start<Startup>(url))
        {                
            Console.WriteLine($"Server running at {{{url}}}");
            Console.ReadLine();
        }
    }

    public static string GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                return ip.ToString();
            }
        }
        throw new Exception("Local IP Address Not Found!");
    }

}