July 2, 2015 11:31 am

Networking API Improvements in Windows 10

In today’s cloud-first, mobile-first technology world, most Store apps have at least some integration with web services or other devices on a network. These include apps that fetch online weather content, news or sports scores, media or podcast downloaders and even peer-to-peer games, chat or VoIP apps. These apps are built using a wide variety of networking APIs that are a part of the Universal Windows Platform.

In Windows 10, our networking team has invested significant effort in improving these APIs in terms of their speed and memory performance as well as the capabilities and flexibility they provide to you, the developer. In this blog post, we’ll discuss some of the key improvements we have made in this area, and illustrate how to leverage these in your own apps.

Networking APIs in Universal Windows Platform

The Universal Windows Platform (UWP) contains a large set of networking APIs that are targeted at diverse scenarios – from querying the connectivity status of the device and connecting to peer devices, to communicating with REST web services and downloading large media files in the background. In this blog post, we’ll focus on the following subset of networking APIs:

  1. Windows.Networking.Sockets – Typically used in P2P chat, VoIP and VPN apps
  2. System.Net.Sockets – .NET version of Sockets API widely used in cross-platform libraries and apps
  3. WinSock – Widely used in cross-platform libraries such as OpenSSL or multiplayer game libraries
  4. Windows.Web.Http and System.Net.Http – Used to communicate with REST web services
  5. Windows.Networking.BackgroundTransfer – Used for uploading or downloading data even when app is not in the foreground
  6. Windows.Data.Json – Used to serialize/deserialize JSON content served by webservices

These APIs represent different layers in networking from Sockets at the lowest layer, HTTP building on top of it and finally, Background Transfer and JSON that add more value building on top of HTTP.

New in Windows 10

In Windows 10, the above-mentioned APIs have been enhanced for greater performance and for providing more flexibility to developers. We discussed all of these improvements in detail in this video for BUILD 2015.

  1. Socket Broker: Allows apps to listen on Sockets even when the app is not in the foreground
  2. Batched Sends: A new optimization in the Sockets API implementation can make sending data up to 4x faster.
  3. HTTP/2: HTTP APIs now support the new, faster version of HTTP protocol for UWP apps. This makes REST webservice downloads and uploads up to 3x faster.
  4. Post-processing in Background Transfer: UWP apps can now handle the completion of their background downloads/uploads in a background task even before the app is launched the next time.

Due to time constraints, we couldn’t cover all of the new features we’re adding to Windows 10 in the video. Here are a few more:

System.Net.Sockets and Winsock for UWP Apps

With Windows 10, System.Net.Sockets and Winsock have been added into the API surface for UWP app developers. These were both highly requested APIs for Windows Store apps (they were already available for Windows Phone Silverlight apps). WinSock is also supported for Windows 8.1 apps with the latest updates to Visual Studio 2014.

The current API surface of both Winsock and System.Net.Sockets for UWP apps is based on that of Phone 8.1 Silverlight and continues to support most of the types, properties and methods (some APIs that are considered obsolete have been removed).

Client certificate support for StreamSocket class

Windows.Networking.StreamSocket class supports using SSL/TLS to authenticate the server the app is talking to. In certain cases, the app also needs to authenticate itself to the server using a TLS client certificate. Starting in Windows 10, you can provide a client certificate on the StreamSocket.Control object (this must be set before the TLS handshake is started). If the server requests the client certificate, Windows will respond with the certificate provided.

Here is a code snippet showing how to implement this:

var socket = new StreamSocket();
Windows.Security.Cryptography.Certificates.Certificate

certificate = await GetClientCert();
socket.Control.ClientCertificate = certificate;

await socket.ConnectAsync(destination, SocketProtectionLevel.Tls12);

Handling null values and better debugging experience for JSON

One of the key developer requests we had for our Windows.Data.Json API in Windows 8.1 was to enable better handling of null values. Starting with Windows 10, we have added new, intuitive APIs to serialize and deserialize null values to and from JSON strings. Here is an example of how you can handle null value in JSON strings in Windows 10 UWP apps:

Serializing an object to JSON (Stringify):

jsonObject[key] = JsonValue.CreateNullValue();
var outputString = jsonObject.Stringify();

Deserializing a JSON string (Parse):

IJsonValue myJsonValue = jsonObject.GetNamedValue(key);

if (myJsonValue.ValueType == JsonValueType.Null)
{
  Phone = null;
}
else
{
  var myValue = myJsonValue.GetString();
}

The new JsonValueType.Null enumeration value enables handling of null values with the same programming patterns that developers use today for integers, strings or Boolean values.

Richer debugging experience in Visual Studio for JSON

The next addition to Windows.Data.Json API is an improved debugging experience with Visual Studio. In Windows 8.1, developers stepping through their code while debugging would only see the type of a JsonObject or JsonArray instance but not its contents. This makes it harder for developers to debug their parsing logic and ensure that their objects have been correctly populated. Starting in Windows 10, you can now inspect the internal contents of JsonObject and JsonArray types. Here’s an example with a JsonArray object:

networking1

Improved SD card support for Background Transfer

In Windows 8.1, the Windows.Networking.BackgroundTransfer API supported downloading data on a removable storage (e.g. SD Card). The design was to download the content in the internal temporary cached storage first and then move the file to the destination. As a result, the size of the file which can be downloaded was constrained by the amount of internal storage available.

In Windows 10, this implementation has been changed to check whether the download is happening in a different drive than where the application is installed and use that location for the temporary cache. This will alleviate the problem of not being able to download large content (content which is greater in size than the available internal storage) on SD card.

Related Content

The entire set of networking APIs in UWP is too large to cover here. You can watch the following videos from Build 2015 for more info on other APIs in the networking area:

  1. .NET Networking APIs for UWP – http://aka.ms/dotnetuwp
  2. Windows.Devices.AllJoyn – https://channel9.msdn.com/Events/Build/2015/2-623
  3. Wi-Fi and Network Connectivity APIs – https://channel9.msdn.com/Events/Build/2015/2-86
  4. Wi-Fi Direct APIs – https://channel9.msdn.com/Events/Build/2015/3-98
  5. Hotspot 2.0 API – https://channel9.msdn.com/Events/Build/2015/2-80
  6. Bluetooth apps – https://channel9.msdn.com/Events/Build/2015/3-739
  7. IoT & Azure integration – https://channel9.msdn.com/Events/Build/2015/2-67

Looking Ahead

With Windows 10, we have evolved our networking APIs from Windows 8.1 to provide better performance and capabilities to you, the developer. Further, since these APIs are a part of UWP, you can use them to build apps for the entire range of Windows devices. With upcoming releases, we will continue to improve the performance of our networking APIs as well as add features that enable developers to build awesome, differentiated experiences on Windows. We look forward to hearing your feedback on which networking APIs are currently missing from UWP and are highest priority for you to deliver your app on Windows. You can file an idea on UserVoice or join the Windows Insiders program and submit feedback through the forums.

Updated January 5, 2016 5:53 pm

Join the conversation

  1. Hi Michael,
    The post for networking APIs in .NET for Windows 10 apps is not yet up on that blog. Once it is, we will update this link. Sorry for the confusion.

    Thanks,
    Sidharth

    • As a question related to this: Is it still impossible to just listen a port, and more so in background?

      • I really hope both of this is possible now.

        “Socket Broker: Allows apps to listen on Sockets even when the app is not in the foreground”

        • Yes – Bruno is right. Socket broker enables you to listen on a socket in the background. For torrents, you will need to implement the protocol yourself on top of a socket. Of course, the background task would be subject to the standard resource budgets for background operations.

          • What exactly are the resource budgets for the background operations. In the case if one wants to create a torrent app then does this mean limited peers or querying of peers?

  2. Are these network features the reason the local network disappears when signed in on a MS Account (all is fine when using a local user)?

    • Peter – this post is about new features we have added to our networking APIs to enable developers to build more performant and full-featured apps. It’s unlikely that this will affect your local network presence.
      I am assuming you are part of the Windows Insider program? For such issues, please use the in-built Windows Feedback to report a bug.

  3. I wish I could set SocketProtectionLevel.Tls12 for Windows.Web.Http.HttpClient, but good that the socket can set the client certificate now. I can use that to implement my own http filter instead of going C++.

    • Hi Windows LiveUser703,

      Can you provide more details as to what you mean by an SIP client? Assuming that you were looking for a way for your app to listen for incoming messages even when the app is not alive, then Yes – Socket Broker does enable that scenario.

      • mh, first comment disapeared.

        Sip is a VOIP protocol defined in https://tools.ietf.org/html/rfc3261
        It is used by quite a few telephone companies even for the last mile, as it is basically telefon via internet.
        Android has a working SIP Client in the OS since 2.3, so I can use it as my main phone at home.

        ATM you can’t write a SIP Client for WP 8.1 as it has to be open and in front to listen on a port.

        If Socket Broker would work in UWP apps on WM10 then we could have a real SIP Client there to.

        • Thanks – yes, in this case, Socket Broker would allow your app to listen on a socket in the background and if there was incoming data, your app would get notified.

          Thanks,
          Sid

          • How does WhatsApp and Vine work on WP 8.1 if it has been introduced in Windows 10?

  4. Hello!
    I would like to know if the Socket Broker is already build in windows 10 for mobile? I ask because I am very interested in having a sip client to get phone calls on wlan even when the phone app is closed. I have windows 10 mobile on my lumia but I can’t find this feature so I think it isn’t build in? Or is it?