> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XBOX 主机端口行为

> XBOX 主机端口行为

本主题介绍 XBOX 主机端口的行为。

## 端口规则

* 除首选本地 UDP (用户数据报协议) 多人游戏端口以外，所有游戏可绑定的 XBOX 主机端口都具有与默认防火墙下 PC 行为一致的行为：允许所有出站 TCP (传输控制协议) 与 UDP 流量，同时阻止未经请求的入站 UDP 与 TCP 数据包。

* 首选本地 UDP 多人游戏端口的行为与其他端口相同，但允许未经请求的入站 UDP 数据包。不能使用 TCP。

* 对于 Microsoft 游戏开发工具包 (GDK) 游戏中的点对点通信，应在首选多人游戏端口上使用 UDP。此外，还必须进行防火墙与 NAT 打洞。

**提示**：[PlayFab Party](/build/console-features/networking/game-mesh/playfab-party-intro-networking) 提供了自动处理此功能的点对点网络拓扑。或者，你也可以以 [STUN](https://tools.ietf.org/html/rfc5389) 和 [ICE](https://tools.ietf.org/html/rfc8445) 的 RFC 作为自行实现的起点。

## 端口可用性/保留端口

游戏可以使用任何可绑定的可用端口。

如果端口已被占用，[bind](https://learn.microsoft.com/windows/desktop/api/winsock/nf-winsock-bind) 函数会返回 `E_ADDRINUSE`，与 PC 上一样。

有一些端口对游戏永远不可用。[保留端口列表](/build/console-features/networking/reserved-ports-networking) 展示了当前由系统保留的端口集合。

Microsoft 游戏开发工具包 (GDK) 游戏无法预先保留端口。在 Microsoft 游戏开发工具包 (GDK) 游戏尝试绑定端口时，该端口始终有可能已被使用。发生端口冲突时需要有合适的回退和重试逻辑。

唯一的例外是 [首选本地 UDP 多人游戏端口](/build/console-features/networking/game-mesh/preferred-local-udp-multiplayer-port-networking)，它为游戏保留，始终保证可用。

## 开发端口

在开发过程中，允许未经请求的入站 TCP 与 UDP 数据包对辅助工具和调试流程可能很有用。Microsoft 游戏开发工具包 (GDK) 游戏想要用于此目的的端口应在 *MicrosoftGame.config* 文件的 `DevelopmentOnly` 节点下声明。可以声明任意数量的端口。这些端口在游戏中的使用方式与其他任何端口相同。开发端口的这种特殊行为仅在开发套件上有效，在零售主机上会恢复为默认行为，即阻止未经请求的入站数据包。以下示例展示了如何在 *MicrosoftGame.config* 中将端口 `4600` 和 `4601` 标记为开发端口。

```xml theme={null}

<?xml version="1.0" encoding="utf-8"?>
<Game configVersion="1">

  ...

  <DevelopmentOnly>
    <DebugNetworkPortList>
      <DebugNetworkPort>4600</DebugNetworkPort>
      <DebugNetworkPort>4601</DebugNetworkPort>
    </DebugNetworkPortList>
  </DevelopmentOnly>
</Game>

```

<Note>修改 *MicrosoftGame.config* 中的 `DebugNetworkPortList` 后，除了重新部署游戏外，还必须运行 [xbapp applyconfig](/tools/tools-console/commandlinetools/xbapp#xbapp_applyconfig) 才能使更改生效。</Note>

## 另请参阅

* [XBOX 保留端口与防火墙例外](/build/console-features/networking/reserved-ports-networking)

* [PC 端口行为](/build/console-features/networking/game-mesh/pc-port-behavior)

* [Windows Sockets 2 (Winsock)](https://learn.microsoft.com/windows/desktop/WinSock/windows-sockets-start-page-2)

* [首选 UDP 多人游戏端口](/build/console-features/networking/game-mesh/preferred-local-udp-multiplayer-port-networking)

* [PlayFab Party](/build/console-features/networking/game-mesh/playfab-party-intro-networking)


## Related topics

- [PC 端口行为](/zh-CN/build/console-features/networking/game-mesh/pc-port-behavior.md)
- [GDK 中的 Windows Sockets 简介](/zh-CN/build/console-features/networking/game-mesh/winsock-intro-networking.md)
- [游戏网状通信：GDK 与 XDK 的差异](/zh-CN/build/console-features/networking/xdk-migration/xdk-migration-game-mesh-networking.md)
- [XBOX 游戏的 Game mesh 网络](/zh-CN/build/console-features/networking/game-mesh/index.md)
- [首选本地 UDP 多人游戏端口网络 API](/zh-CN/build/console-features/networking/game-mesh/preferred-local-udp-multiplayer-port-networking.md)
