Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
StreamPeerTCP
继承: StreamPeerSocket < StreamPeer < RefCounted < Object
处理 TCP 连接的流对等端。
描述
处理 TCP 连接的流对等端。该对象可用于连接 TCP 服务器,也可以由 TCP 服务器返回。
注意:导出到安卓时,在导出项目或使用一键部署之前,请务必在安卓导出预设中,开启 INTERNET 权限。否则,任何类型的网络通信都将被 Android 阻止。
方法
connect_to_host(host: String, port: int) |
|
get_connected_host() const |
|
get_connected_port() const |
|
get_local_port() const |
|
void |
set_no_delay(enabled: bool) |
方法说明
Error bind(port: int, host: String = "*") 🔗
打开 TCP 套接字,并将其绑定到指定的本地地址。
通常不需要这个方法,只是用来强制让后续调用 connect_to_host() 时使用指定的主机 host 和端口 port 作为源地址。会在部分 NAT 打洞技术中用到,也可用于强制设置源网络接口。
Error connect_to_host(host: String, port: int) 🔗
连接到指定的 host:port 对。如果使用的是有效主机名,则会进行解析。成功时返回 @GlobalScope.OK。
String get_connected_host() const 🔗
返回该对等端的IP。
int get_connected_port() const 🔗
返回该对等端的端口。
返回该对等端绑定到的本地端口。
void set_no_delay(enabled: bool) 🔗
如果 enabled 为 true,数据包将被立即发送。如果 enabled 为 false(默认值),数据包的传输将会被延迟,并通过 纳格算法(Nagle's algorithm) 进行合并发送。
注意: 对于需要发送大型数据包或传输大量数据的应用程序,建议保持此选项为禁用状态,因为启用它可能会降低总的可用带宽。