Creating a web server in C# UWP

前端 未结 3 1151
臣服心动
臣服心动 2021-01-12 04:58

I am writing a web server as a Universal Windows Platform app in C#. Here is my code so far:

sealed partial class App : Application
    {
        int port =          


        
3条回答
  •  孤街浪徒
    2021-01-12 05:09

    If you want to host a server in uwp app, be sure these things:

    1. your device which run this code (Device A) and device which your web browser run (Device B) must at a same LAN. And you cannot use the browser in Device A to access your service.
    2. use WIFI to access your service.
    3. your app must be at the state of running.
    4. you should write a method to get ip address, but not 127.0.0.1:

      public static string FindIPAddress()
      {
          List ipAddresses = new List();
          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];
          }
      }
      

      It is possible to host a web service on phone/tablet.

提交回复
热议问题