【问题标题】:SignalR: can't connect to local or any other ip addressSignalR:无法连接到本地或任何其他 IP 地址
【发布时间】:2018-05-15 17:51:25
【问题描述】:

我正在尝试制作 SignalR 服务器和客户端架构,在该架构中我可以连接到“http://localhost:8080”或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();
            }
        }

    }

【问题讨论】:

    标签: asp.net-mvc c#-4.0 server signalr signalr-hub


    【解决方案1】:

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

    终于我发现了只与权限有关的问题。 以管理员身份运行您的 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!");
        }
    
    }
    

    【讨论】:

    • 你在开玩笑吧!我一直在寻找这个解决方案几个月......我最终通过 IIS 托管,但重新审视了这个问题,因为我知道必须有一个简单的解决方案,而且确实在这里。
    【解决方案2】:

    要获取您的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");  
    

    我希望这会有所帮助。

    【讨论】:

    • 对不起,我也面临同样的问题
    • 我在这里得到了样本code.msdn.microsoft.com/windowsdesktop/…
    • 我又试了一次,它对我有用:将 const string ServerURI 替换为 string ServerURI =String.Concat("http://",GetLocalFQDN(),":8080");
    • 查看我对 GetLocalFQDN() 的评论,要使用它,您还需要使用 System.Net.NetworkInformation;
    • 感谢您的帮助...但仍然面临同样的问题...我遇到了“System.Reflection.TargetInvocationException”异常...我在code.msdn.microsoft.com/windowsdesktop/… 上获得了此示例。 .
    【解决方案3】:

    由于您使用的是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 类中完成。

    【讨论】:

    • 对不起,兄弟...这对我不起作用,因为我得到了 ServerURI= "192.*.*.*:8080" 的值,这不是在工作,而是为 "127.0.0.1:8080 “它正在工作..ipaddress可能会出现问题......非常感谢你的帮助......现在我正在寻找其他解决方案......如果你有工作代码,你可以给那个样本。 .
    • 实际上我正在调试 GetLocalIPAddress() 方法,其中我遇到了 system.net.sockets.socketException 异常 .... 我认为这可能是我的问题的原因..让我检查一下希望它会得到解决
    • 我有你的样品和我发送的样品。祝你一切顺利:)
    • 是的......它正在工作,只是我必须在管理员模式下运行 Visual Studio......花了很多时间......thanx @etr
    • 它在我的电脑上工作......服务器和客户端正在同一台电脑上连接......但是如果我在任何其他电脑上运行服务器,而在另一台通过局域网连接的电脑上运行客户端......然后它不起作用(我已将服务器 IP 地址放在客户端 url 中)实际上我是新手,所以我必须在另一台机器上运行服务器并在另一台机器上运行客户端......我该怎么做?
    猜你喜欢
    • 2011-06-06
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多