2016-06-29 1 views
2

"http://localhost:8080"또는 http://127.0.0.1:8080/에 연결할 수 있지만 로컬 IP 주소를 연결할 수없는 SignalR 서버 및 클라이언트 아키텍처를 만들려고합니다. "192. xxx"와 같은 이유가 무엇일까요? ... 나는 또한 내 코드 overhere를 배치하고 저를 도와주세요SignalR : 로컬 또는 다른 IP 주소에 연결할 수 없습니다.

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(); 
      } 
     } 

    } 

답변

0

얻을 수 있도록 local IP address이 기능을 사용할 수 있습니다 : 당신이 FQDN 사용하려면

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!"); 
     } 

을 - 정규화 된 도메인 이름을, 다음은이 기능을 사용할 수 있습니다 : 그 후

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

를 다음과 같이 사용할 수 있습니다

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

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

또는

나는이 도움이되기를 바랍니다.

+0

감사합니다. 동일한 문제가 있었지만 해결되었습니다. – drgmak

+0

미안하지만 나에게 똑같은 문제가있다. –

+0

나는 샘플을 가지고있다. https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b –

0

너도 this source을 사용하고 있기 때문에 동일한 것을 사용했습니다.
-FQDN의 경우 먼저 아래 함수를 만듭니다.

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");에 변경 :

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://",GetLocalIPAddress(),":8080"); 

희망이 도움이됩니다.

참고 :WinFormsServer 프로젝트의 WinFormsServer:Form 클래스에서 변경해야합니다.

+0

죄송합니다 형제 ... ServerURI = "http : //192.*.*.* : 8080"의 가치를 얻고 있기 때문에이 기능이 작동하지 않습니다. 그러나 "http://127.0.0.1 : 8080 "작동 중입니다 ... 가능한 문제가 ipaddress와 함께있을 수도 있습니다 ... UR 도움에 너무 많은 형제님께 감사드립니다 ... 지금 다른 해결책을 찾고 있습니다 ... 만약 당신이 해당 코드를 제공 할 수 있습니까? .. –

+0

실제로 내가 system.net.sockets.socketException 예외를 가지고 GetLocalIPAddress() 메서드를 디버깅했다 .... 내 probelm..let의 이유가 될 수있을 것 같아요 내게 다시 얻을 희망을 확인 –

+0

제가 보낸 샘플을 가지고 샘플을 가지고 있습니다. 당신에게 최선을 기원합니다. – eg16

관련 문제