CX Developre
CX Developre

Reputation: 63

UWP Cliet and Server

I have the UWP app("server") running on win10 prof, this UWP app is an application service and I have another UWP app(Client), which communicates with that service using TSP/IP. How many clients running on the other win10 devices can connect to the "server" at the same time?

Upvotes: 0

Views: 381

Answers (1)

Jared
Jared

Reputation: 784

Using the StreamSocketListener class, I believe you can handle an unlimited number of client socket connections (depending on implementation, hardware, bandwidth, etc.). Here's a basic example of the server-side implementation of the listener using a static class.

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

There are different ways of handling stream sockets once you have both ends connected, and I had to do some research and experimentation to find a process that worked for what I was doing.

Upvotes: 1

Related Questions