Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用windows 10通用应用程序进行客户端-服务器编程_C#_Wcf_Client Server_Win Universal App - Fatal编程技术网

C# 使用windows 10通用应用程序进行客户端-服务器编程

C# 使用windows 10通用应用程序进行客户端-服务器编程,c#,wcf,client-server,win-universal-app,C#,Wcf,Client Server,Win Universal App,我想创建一个客户机/服务器系统,其中客户机是收集数据的C Windows 10通用应用程序,服务器是某种形式的C程序,可以对客户机进行身份验证,发送和接收收集的数据,等等 我已经编写了客户端通用应用程序的基本部分,现在需要做网络部分。有人能提供一个框架+示例,说明如何构建服务器以连接到windows 10通用应用程序吗?我正在调查windows communication framework,但没有找到任何将其集成到通用应用程序的示例。您可以选择,在实现服务器以支持通用应用程序客户端时没有特别考

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

我已经编写了客户端通用应用程序的基本部分,现在需要做网络部分。有人能提供一个框架+示例,说明如何构建服务器以连接到windows 10通用应用程序吗?我正在调查windows communication framework,但没有找到任何将其集成到通用应用程序的示例。

您可以选择,在实现服务器以支持通用应用程序客户端时没有特别考虑,客户端需要支持通用应用程序所使用的协议。有一个非常古老的示例,但它也应该适用于今天的通用应用程序


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

以下是我用于应用程序的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;
      }
 }