2016-06-29 24 views
2

Ich versuche SignalR Server- und Client-Architektur zu machen, in der ich eine Verbindung zu "http://localhost:8080" oder http://127.0.0.1:8080/ herstellen kann, aber ich kann meine lokale IP-Adresse nicht verbinden wie "192. xxx" also was könnte Grund sein? bitte helfen Sie mir, ich bin auch platzieren meinen Code over ...SignalR: kann keine Verbindung zu lokalen oder einer anderen IP-Adresse herstellen

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

    } 

Antwort

0

Um Ihre local IP address Sie diese Funktion nutzen zu können:

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

Wenn Sie FQDN verwenden möchten - Fully Qualified Domain Name, dann könnten Sie mit dieser Funktion:

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

danach könnten Sie:

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

oder

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

Ich hoffe, das hilft.

+0

Danke, ich hatte das gleiche Problem und das hat es gelöst. – drgmak

+0

Entschuldigung, aber für mich bin ich mit dem gleichen Problem konfrontiert –

+0

Ich habe die Probe overhere https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b –

0

Da Sie this source verwenden, habe ich das gleiche auch verwendet.
- Erstellen Sie für FQDN zuerst die folgende Funktion.

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

ändern Dann wird die const string ServerURI zu:

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

-Für LocalIPAdress, zunächst die Funktion unterhalb derer schaffen die Ihre lokale Adresse:

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

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

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

Hoffe das hilft dir.

Hinweis: Die Änderungen sollten in der WinFormsServer:Form Klasse am WinFormsServer Projekt durchgeführt werden.

+0

Entschuldigung Bruder ... das funktioniert nicht für mich, weil ich Wert von ServerURI = "http: //192.*.*.*: 8080" bekomme, was nicht woking aber für "http://127.0.0.1: 8080 "es funktioniert ... es könnte möglich sein, dass Probleme mit ipaddress ... Vielen Dank bro für Ihre Hilfe ... Nowi bin auf der Suche nach anderen Lösungen ... Wenn Sie Arbeitscode haben können Sie diese Probe geben .. –

+0

eigentlich war ich Debug GetLocalIPAddress() -Methode, in der ich eine Ausnahme von system.net.sockets.SocketException .... ich denke, es könnte der Grund für meine problemm sein .. lass mich prüfen, hoffe, es wird –

+0

gelöst werden Ich habe die Probe, die Sie mit den Proben haben, die ich sendete. Wünsche dir das Beste :) – eg16