【问题标题】:client server programming with windows 10 universal apps使用 Windows 10 通用应用程序进行客户端服务器编程
【发布时间】:2015-12-23 05:24:25
【问题描述】:

我想创建一个客户端/服务器系统,其中客户端是一个 (C#) Windows 10 通用应用程序,用于收集数据,而服务器是一个 C# 程序(某种形式),可以对客户端进行身份验证、发送和接收收集到的数据等。

我已经编写了客户端通用应用程序的基本框架,现在需要做网络部分。谁能建议一个框架+如何构建服务器以连接到 Windows 10 通用应用程序的示例?我正在研究 windows 通信框架,但我没有找到任何如何将它们集成到通用应用程序中的示例。

【问题讨论】:

    标签: c# wcf client-server win-universal-app


    【解决方案1】:

    你可以用WCF,实现服务端支持通用应用客户端没有特殊考虑,客户端需要支持通用应用做的协议。有一个相当老的示例here,但它也应该适用于当今的通用应用程序。

    您需要记住的一件事是,如果您要将应用程序发布到应用商店,您的应用程序和服务器不能在同一台机器上运行,因为不允许 Windows 应用商店应用程序连接到 localhost。

    【讨论】:

      【解决方案2】:

      这是我用于应用程序的 StreamSocketListener 类的服务器端实现的基本示例。我在这个例子中使用了一个静态类。您还需要客户端的逻辑。

      如果您需要将数据发送回客户端,可以将每个客户端套接字添加到以 IP(或其他标识符)为键的字典集合中。

      希望有帮助!

      // 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(ViewModel.Current.Port); // Your port goes here.
                 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
            {
                // Authenticate client here, then handle communication if successful.  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;
            }
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-06
        • 2010-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-27
        • 1970-01-01
        相关资源
        最近更新 更多