Sdílet prostřednictvím


Work with networking in your DirectX game

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Learn how to develop and incorporate networking features into your DirectX game.

Concepts at a glance

A variety of networking features can be used in your DirectX game, whether it is a simple standalone game to massively multi-player games. The simplest use of networking would be to store user names and game scores on a central network server.

Networking APIs are needed in multi-player games that use the infrastructure (client-server or internet peer-to-peer) model and also by ad hoc (local peer-to-peer) games. For server-based multi-player games, a central game server usually handles most of the game operations and the client game app is used for input, displaying graphics, playing audio, and other features. The speed and latency of network transfers is a concern for a satisfactory game experience.

For peer-to-peer games, each player's app handles the input and graphics. In most cases, the game players are located in close proximity so that network latency should be lower but is still a concern. How to discovery peers and establish a connection becomes a concern.

For single-player games, a central Web server or service is often used to store user names, game scores, and other miscellaneous information. In these games, the speed and latency of networking transfers is less of a concern since it doesn't directly affect game operation.

Network conditions can change at any time, so any game that uses networking APIs needs to handle network exceptions that may occur. See Handling exceptions in network apps to learn more about handling network exceptions.

Firewalls and web proxies are common and can affect the ability to use networking features. A game that uses networking needs to be prepared to properly handle firewalls and proxies.

For mobile devices, it is important to monitor available network resources and behave accordingly when on metered networks where roaming or data costs can be significant.

Network isolation is part of the app security model used by Windows RT 8.1. Windows actively discovers network boundaries and enforces network access restrictions for network isolation. Apps must declare network isolation capabilities in order to define the scope of network access. Without declaring these capabilities, your app will not have access to network resources. See How to configure network isolation capabilities to learn more about how Windows enforces network isolation for apps.

Design considerations

A variety of networking APIs can be used in DirectX games. So it is important to pick the right API. Windows RT 8.1 supports a variety of networking APIs that your app can use to communicate with other computers and devices over either the Internet or private networks. Your first step is to figure out what networking features your app needs.

The more popular network APIs for games include:

  • TCP and sockets - Provides a reliable connection. Use TCP for game operations that don’t need security. TCP allows the server to easily scale, so it is commonly used in games that use the infrastructure (client-server or internet peer-to-peer) model. TCP can also be used by ad hoc (local peer-to-peer) games over Wi-Fi Direct and Bluetooth. TCP is commonly used for game object movement, character interaction, text chat, and other operations. The StreamSocket class provides a TCP socket that can be used in Windows Store games. The StreamSocket class is used with related classes in the Windows::Networking::Sockets namespace.
  • TCP and sockets using SSL - Provides a reliable connection that prevents eavesdropping. Use TCP connections with SSL for game operations that need security. The encryption and overhead of SSL adds a cost in latency and performance, so it is only used when security is needed. TCP with SSL is commonly used for login, purchasing and trading assets, game character creation and management. The StreamSocket class provides a TCP socket that supports SSL.
  • UDP and sockets - Provides unreliable network transfers with low overhead. UDP is used for game operations that require low latency and can tolerate some packet loss. This is often used for fighting games, shooting and tracers, network audio, and voice chat. The DatagramSocket class provides a UDP socket that can be used in Windows Store games. The DatagramSocket class is used with related classes in the Windows::Networking::Sockets namespace.
  • HTTP Client - Provides a reliable connection to HTTP servers. The most common networking scenario is to access a web site to retrieve or store information. A simple example would be a game that uses a website to store user information and game scores. When used with SSL for security, an HTTP client can be used for login, purchasing, trading assets, game character creation, and management. The HttpClient class provides a modern HTTP client API for use in Windows Store games. The HttpClient class is used with related classes in the Windows::Web::Http namespace.

In this section

Topic Description

Handle network exceptions in your DirectX game

Learn how to handle network exceptions when using networking APIs in your DirectX game.

 

Other resources

Connecting with a datagram socket

Connecting to a network resource with a stream socket

Connecting to network services

Connecting to web services

Handling exceptions in network apps

How to configure network isolation capabilities

How to enable loopback and debug network isolation

Staying connected in the background

Reference

DatagramSocket

HttpClient

StreamSocket

Windows::Web::Http

Windows::Networking::Sockets

Samples

DatagramSocket sample

HttpClient Sample

Proximity sample

StreamSocket sample