- Blocking: XNetworkingQueryPreferredLocalUdpMultiplayerPort
- Asynchronous: XNetworkingQueryPreferredLocalUdpMultiplayerPortAsync
- Notification-based: XNetworkingRegisterPreferredLocalUdpMultiplayerPortChanged
INADDR_ANY/in6addr_any address.
Handling socket failures
There’s no guarantee that the returned port can be used to establish a successful socket connection with particular servers or peers. Normal title retry and fallback logic should be performed. Whenever the socket is closed and reopened, the title should query again for the most recent, preferred local UDP multiplayer port, because the port can change over time.Network initialization
All three (blocking, asynchronous, and notification-based) variants of theXNetworkingQueryPreferredLocalUdpMultiplayerPort API will block or delay completion/notifications until the network is initialized on title launch and on resume. You may separately wait for network initialization according to the Detecting network initialization status overview, or may make a call to these APIs and wait for them to return.
Suspend and resume
Just like any other socket, the socket bound to the preferred local UDP multiplayer port should be closed on suspend and re-created on resume after waiting for network initialization. You should register for suspend and resume events viaRegisterAppStateChangeNotification. On resume, you should assume that the preferred local UDP multiplayer port has changed and either be listening for changes to the preferred local UDP multiplayer port or re-query when creating your new sockets. For more information on WinSock suspend and resume handling, see Suspend and resume in Winsock.
Changes in the preferred local UDP multiplayer port
The title can listen for changes in the preferred local UDP multiplayer port by using the XNetworkingRegisterPreferredLocalUdpMultiplayerPortChanged API. All attempts are made to ensure that the preferred local UDP multiplayer port doesn’t change while the title is running. However, there are unavoidable cases where the port will change due to the user’s external network conditions changing and invalidating any existing socket flows. The port is especially likely to change when the network connectivity level changes, or as part of a title’s suspend/resume cycle. When the preferred local UDP multiplayer port changes, additional inbound connections from future peers might get blocked on any previous preferred port. This might not cause a failure at the socket layer. However, the title might eventually stop receiving packets on any socket bound to any previous preferred port. Packets sent to and from existing peers might continue to function. A notification about a change in the preferred local UDP multiplayer port might not be fatal to any in-progress game session. When a change notification occurs, the title should migrate to a new socket bound on the new preferred port. This migration should happen at the earliest opportunity and without interrupting any existing gameplay. To detect a connection loss and to retry the socket connection, the title should always use the most recent preferred port.Testing changes to the preferred local UDP multiplayer port
Use the following steps to change the preferred local UDP multiplayer port.- While your game is running, open the XBOX Guide. Go to the Settings app.
- On the General tab, select Network settings.
- Select Advanced settings, and then select Alternate port selection.
- Set the port selection to Manual. To select a port, use the drop-down menu.
- Port selection immediately takes effect and causes a corresponding notification to your title.
- When you’re done testing, set the port selection back to Automatic to return the port behavior back to the default.
While in the Settings app, your title is constrained but still running and immediately receives the port change notification, even though your title isn’t visible. Your title is suspended if you leave the Settings app open for longer than 10 minutes without switching back to your title.
Security
The socket bound to the preferred local UDP multiplayer port behaves just as any other socket would. In particular, the socket doesn’t provide any additional security. The title should use its own secure communication protocol on top of the socket that’s bound to the preferred local UDP multiplayer port as specified by the communication security best practices. See Communication Security Overview (NDA topic) for more information.Peer-to-peer
The preferred local UDP multiplayer port provides the best-known port that a peer-to-peer mesh can be built from. It’s configured in the best possible way to allow inbound connections through the user’s NAT layer. However, it’s the responsibility of the title to perform NAT traversal, including the following.- Detection of the NAT type
- Detection and exchange of the device’s public IP address and port
- NAT punching and traversal
