【问题标题】:Creating a web server in C# UWP在 C# UWP 中创建 Web 服务器
【发布时间】:2016-02-07 03:32:46
【问题描述】:

我正在用 C# 编写一个 Web 服务器作为通用 Windows 平台应用程序。到目前为止,这是我的代码:

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# 代码的问题还是其他问题。

【问题讨论】:

  • 你看过这个:loopback.codeplex.com 吗? WinRT 和 UWP 应用具有环回保护,该工具将为指定的应用删除它。也许这就是你所需要的。
  • 不幸的是,环回异常仅适用于客户端套接字

标签: c# webserver uwp


【解决方案1】:

Raymond Zuo 的解决方案确实有效。但不要忘记的主要内容是 Packages.appxmanifest 中的功能。为了在专用网络中运行服务器,应添加:

<Capability Name="privateNetworkClientServer" />

为了在公共网络中运行服务器:

<Capability Name="internetClientServer" />

【讨论】:

    【解决方案2】:

    如果您想在 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];
          }
      }
      

      可以在手机/平板电脑上托管网络服务。

    【讨论】:

    • 您有一个如何在 WinRT 中托管 Web 服务的示例吗?我发现的一切都不起作用
    • 我喜欢 "else if" "else" 毫无意义;-D
    【解决方案3】:

    可以在 Window 通用应用程序中托管 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 ,我也无法在 localhost 上运行。我目前正在对通用平台应用程序执行 java http 请求。当然,服务器和客户端似乎需要在不同的主机上运行。

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      相关资源
      最近更新 更多