简体   繁体   中英

client server programming with windows 10 universal apps

I would like to create a client/server system, where the client is a (C#) Windows 10 universal app that gathers data, and the server is a C# program (of some form) that can authenticate the client, send and receive the gathered data, etc.

I have written the bare bones of the client universal app, and now need to do the network part. Can anyone suggest a framework + examples of how to build a server to connect to windows 10 universal apps? I was investigating the windows communication framework, but I haven't found any examples of how to integrate them into a universal app.

You can go with WCF , there is no special consideration in implementing the server to support a Universal App client, client needs to support the protocol which universal apps do. There is a pretty old sample here but it should work with today's universal apps too.

One thing that you need to keep in mind is that if you are going to publish your app into the Store, your app and the server cannot be running on the same machine since Windows Store Apps are not allowed to connect to localhost.

Here's a basic example of the server-side implementation of the StreamSocketListener class that I used for an app. I used a static class in this example. You'll also need logic for the client side too.

If you need to send data back to the clients, you could add each client socket to a dictionary collection with the IP (or some other identifier) as the key.

Hope that helps!

// 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;
      }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM