2015-11-06 66 views
6

web服務器我寫一個Web服務器在C#通用Windows平臺的應用程序。這是我到目前爲止的代碼:創建在C#UWP

sealed partial class App : Application 
    { 
     int port = 8000; 

     /// <summary> 
     /// Initializes the singleton application object. This is the first line of authored code 
     /// executed, and as such is the logical equivalent of main() or WinMain(). 
     /// </summary> 
     public App() 
     { 
      StartServer(); 
     } 

     private void StartServer() 
     { 
      StreamSocketListener listener = new StreamSocketListener(); 
      listener.BindServiceNameAsync(port.ToString()); 
      Debug.WriteLine("Bound to port: " + port.ToString()); 
      listener.ConnectionReceived += async (s, e) => 
       { 
        Debug.WriteLine("Got connection"); 
        using (IInputStream input = e.Socket.InputStream) 
        { 
         var buffer = new Windows.Storage.Streams.Buffer(2); 
         await input.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.Partial);  
        } 

        using (IOutputStream output = e.Socket.OutputStream) 
        { 
         using (Stream response = output.AsStreamForWrite()) 
         { 
          response.Write(Encoding.ASCII.GetBytes("Hello, World!"), 0, 1); 
         } 
        } 
       }; 
     } 
    } 

我嘗試使用這個地址連接到服務器:

http://127.0.0.1:8000/C:/pathtohtmlfile/htmlfile.html

然而,連接超時。我不確定這是C#代碼還是其他問題。

+0

你看這個:http://loopback.codeplex.com/? WinRT和UWP應用程序具有環回保護,該工具將爲指定的應用程序刪除它。也許這是你需要的。 – Rafael

+0

環回例外是客戶端套接字只不幸 –

回答

5

如果你想舉辦在UWP應用程序的服務器,可以肯定這些東西:

  1. 的裝置並運行此代碼(設備A)和裝置,您的Web瀏覽器上運行(設備B)必須在同一個LAN。並且您無法使用設備A中的瀏覽器訪問您的服務。
  2. 使用WIFI訪問您的服務。
  3. 您的應用程序必須運行的狀態。
  4. 你應該寫一個方法來獲得IP地址,而不是127.0.0.1:

    public static string FindIPAddress() 
    { 
        List<string> ipAddresses = new List<string>(); 
        var hostnames = NetworkInformation.GetHostNames(); 
        foreach (var hn in hostnames) 
        { 
         //IanaInterfaceType == 71 => Wifi 
         //IanaInterfaceType == 6 => Ethernet (Emulator) 
         if (hn.IPInformation != null && 
          (hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71 
          || hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6)) 
         { 
          string ipAddress = hn.DisplayName; 
          ipAddresses.Add(ipAddress); 
         } 
        } 
    
        if (ipAddresses.Count < 1) 
        { 
         return null; 
        } 
        else if (ipAddresses.Count == 1) 
        { 
         return ipAddresses[0]; 
        } 
        else 
        { 
         return ipAddresses[ipAddresses.Count - 1]; 
        } 
    } 
    

    而且我不知道爲什麼有人給這個問題下投票,是否有可能上託管Web服務手機/平板電腦,當然是的。

+0

你有怎樣主辦的WinRT一個Web服務的例子嗎?我找到的所有東西根本不起作用 – CodeNoob

+0

我喜歡「其他」如何「別的」,沒有任何意義; -D –

4

Raymond Zuo的解決方案確實有效。但不要忘記的主要是Packages.appxmanifest中的功能。爲了運行在專用網絡中的服務器應該加上:

<Capability Name="privateNetworkClientServer" /> 

而且爲了運行在公共網絡服務器:

<Capability Name="internetClientServer" /> 
1

有可能舉辦一個窗口通用Web服務應用。我遵循http://www.dzhang.com/blog/2012/09/18/a-simple-in-process-http-server-for-windows-8-metro-apps的例子,遵循了Raymond Zuo解決方案的三個第一步,最後我還放下了防火牆。不幸的是,即使我遵循了這裏的答案Cannot connect to localhost in windows store application,我仍然無法在本地主機上運行。我目前正在對通用平臺應用程序進行java http請求。當然,服務器和客戶端似乎需要在不同的主機上運行。