UWP Cliet和服务器

时间:2015-11-27 07:47:28

标签: win-universal-app

我有在win10教程上运行的UWP应用程序("服务器"),这个UWP应用程序是一个应用程序服务,我有另一个UWP应用程序(客户端),它使用TSP / IP与该服务通信。在其他win10设备上运行的客户端数量可以连接到"服务器"同时?

1 个答案:

答案 0 :(得分:0)

使用StreamSocketListener类,我相信您可以处理无限数量的客户端套接字连接(取决于实现,硬件,带宽等)。这是使用静态类的侦听器的服务器端实现的基本示例。

// Define static class here.

public static StreamSocketListener Listener { get; set; }

// This is the static method used to start listening for connections.

 public static async Task<bool> StartServer()
 {
      Listener = new StreamSocketListener();
      // Removes binding first in case it was already bound previously.
      Listener.ConnectionReceived -= Listener_ConnectionReceived;
      Listener.ConnectionReceived += Listener_ConnectionReceived;
      try
      {
           await Listener.BindServiceNameAsync(VMS.Current.Port);
           return true;
       }
       catch (Exception ex)
       {
          Listener.ConnectionReceived -= Listener_ConnectionReceived;
          Listener.Dispose();
          return false;
        }
 }

 private static async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
 {
      var remoteAddress = args.Socket.Information.RemoteAddress.ToString();
      var reader = new DataReader(args.Socket.InputStream);
      var writer = new DataWriter(args.Socket.OutputStream);
      try
      {
          // Handle communication here.  You'll likely use an infinite loop of reading from the input stream until the socket is disconnected.
      }
      catch (Exception ex)
      {
           writer.DetachStream();
           reader.DetachStream();
           return;
      }
 }

一旦你连接了两端,有不同的方法来处理流套接字,我不得不做一些研究和实验来找到一个适合我正在做的事情的过程。