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.

PacketPeerUDP

繼承: PacketPeer < RefCounted < Object

UDP 資料包使用者端。

說明

UDP packet peer. Can be used to send and receive raw UDP packets as well as Variants.

Example: Send a packet:

var peer = PacketPeerUDP.new()

# Optionally, you can select the local port used to send the packet.
peer.bind(4444)

peer.set_dest_address("1.1.1.1", 4433)
peer.put_packet("hello".to_utf8_buffer())

Example: Listen for packets:

var peer

func _ready():
    peer = PacketPeerUDP.new()
    peer.bind(4433)


func _process(_delta):
    if peer.get_available_packet_count() > 0:
        var array_bytes = peer.get_packet()
        var packet_string = array_bytes.get_string_from_ascii()
        print("Received message: ", packet_string)

Note: When exporting to Android, make sure to enable the INTERNET permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android.

方法

Error

bind(port: int, bind_address: String = "*", recv_buf_size: int = 65536)

void

close()

Error

connect_to_host(host: String, port: int)

int

get_local_port() const

String

get_packet_ip() const

int

get_packet_port() const

bool

is_bound() const

bool

is_socket_connected() const

Error

join_multicast_group(multicast_address: String, interface_name: String)

Error

leave_multicast_group(multicast_address: String, interface_name: String)

void

set_broadcast_enabled(enabled: bool)

Error

set_dest_address(host: String, port: int)

Error

wait()


方法說明

Error bind(port: int, bind_address: String = "*", recv_buf_size: int = 65536) 🔗

Binds this PacketPeerUDP to the specified port and bind_address with a buffer size recv_buf_size, allowing it to receive incoming packets.

If bind_address is set to "*" (default), the peer will be bound on all available addresses (both IPv4 and IPv6).

If bind_address is set to "0.0.0.0" (for IPv4) or "::" (for IPv6), the peer will be bound to all available addresses matching that IP type.

If bind_address is set to any valid address (e.g. "192.168.1.101", "::1", etc.), the peer will only be bound to the interface with that address (or fail if no interface with the given address exists).


void close() 🔗

關閉該 PacketPeerUDP 底層 UDP 通訊端。


Error connect_to_host(host: String, port: int) 🔗

呼叫該方法將該 UDP 對等體連接到給定的 host/port 對。UDP 實際上是不需連線的,因此該選項僅意味著自動丟棄來自不同位址的傳入封包,並且傳出的封包始終發送到連接的位址(不允許將來呼叫 set_dest_address())。該方法不會向遠端對等體發送任何資料,要發送資料,請像往常一樣使用 PacketPeer.put_var()PacketPeer.put_packet()。另請參閱 UDPServer

注意:連接到遠端對等體並不能防止 IP 欺騙等惡意攻擊。如果你覺得你的套用程式正在傳輸敏感資訊,可以考慮使用 TLS 或 DTLS 等加密技術。


int get_local_port() const 🔗

返回該對等體綁定到的本地埠。


String get_packet_ip() const 🔗

返回發送最後一個封包(通過 PacketPeer.get_packet()PacketPeer.get_var() 接收)的遠程對等體的 IP。


int get_packet_port() const 🔗

返回發送最後一個封包(通過 PacketPeer.get_packet()PacketPeer.get_var() 接收)的遠端對等方的埠。


bool is_bound() const 🔗

返回該 PacketPeerUDP 是否被綁定到某個位址上,並且可以接收封包。


bool is_socket_connected() const 🔗

如果 UDP 通訊端已打開並已連接到遠端位址,則返回 true。見 connect_to_host()


Error join_multicast_group(multicast_address: String, interface_name: String) 🔗

使用由 interface_name 標識的介面加入由 multicast_address 指定的多播組。

可以使用多個介面加入同一個多播組。使用 IP.get_local_interfaces() 瞭解哪些介面可用。

注意:某些 Android 裝置可能需要 CHANGE_WIFI_MULTICAST_STATE 許可權才能進行多播。


Error leave_multicast_group(multicast_address: String, interface_name: String) 🔗

multicast_address 指定的組播組中移除 interface_name 標識的介面。


void set_broadcast_enabled(enabled: bool) 🔗

啟用或禁用廣播封包的發送(例如:set_dest_address("255.255.255.255", 4343)。這個選項在預設情況下是禁用的。

注意:一些 Android 裝置可能需要 CHANGE_WIFI_MULTICAST_STATE 許可權和本選項被啟用來接收廣播包。


Error set_dest_address(host: String, port: int) 🔗

設定發送封包和變數的目標位址和埠。如果需要,將使用 DNS 解析主機名稱。

注意:在向廣播地址(例如:255.255.255.255)發送封包之前,必須啟用 set_broadcast_enabled()


Error wait() 🔗

Waits for a packet to arrive on the bound address. See bind().

Note: wait() can't be interrupted once it has been called. This can be worked around by allowing the other party to send a specific "death pill" packet like this:

socket = PacketPeerUDP.new()
# Server
socket.set_dest_address("127.0.0.1", 789)
socket.put_packet("Time to stop".to_ascii_buffer())

# Client
while socket.wait() == OK:
    var data = socket.get_packet().get_string_from_ascii()
    if data == "Time to stop":
        return