Table of Contents

Class PacketPeerUdp

Namespace
Godot
Assembly
GodotSharp.dll

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

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.

[GodotClassName("PacketPeerUDP")]
public class PacketPeerUdp : PacketPeer, IDisposable
Inheritance
PacketPeerUdp
Implements
Inherited Members

Constructors

PacketPeerUdp()

public PacketPeerUdp()

Methods

Bind(int, string, int)

Binds this PacketPeerUdp to the specified port and bindAddress with a buffer size recvBufSize, allowing it to receive incoming packets.

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

If bindAddress 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 bindAddress 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 addresses (or fail if no interface with the given address exists).

public Error Bind(int port, string bindAddress = "*", int recvBufSize = 65536)

Parameters

port int
bindAddress string
recvBufSize int

Returns

Error

Close()

Closes the PacketPeerUdp's underlying UDP socket.

public void Close()

ConnectToHost(string, int)

Calling this method connects this UDP peer to the given host/port pair. UDP is in reality connectionless, so this option only means that incoming packets from different addresses are automatically discarded, and that outgoing packets are always sent to the connected address (future calls to SetDestAddress(string, int) are not allowed). This method does not send any data to the remote peer, to do that, use PutVar(Variant, bool) or PutPacket(byte[]) as usual. See also UdpServer.

Note: Connecting to the remote peer does not help to protect from malicious attacks like IP spoofing, etc. Think about using an encryption technique like TLS or DTLS if you feel like your application is transferring sensitive information.

public Error ConnectToHost(string host, int port)

Parameters

host string
port int

Returns

Error

GetLocalPort()

Returns the local port to which this peer is bound.

public int GetLocalPort()

Returns

int

GetPacketIP()

Returns the IP of the remote peer that sent the last packet(that was received with GetPacket() or GetVar(bool)).

public string GetPacketIP()

Returns

string

GetPacketPort()

Returns the port of the remote peer that sent the last packet(that was received with GetPacket() or GetVar(bool)).

public int GetPacketPort()

Returns

int

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsBound()

Returns whether this PacketPeerUdp is bound to an address and can receive packets.

public bool IsBound()

Returns

bool

IsSocketConnected()

Returns true if the UDP socket is open and has been connected to a remote address. See ConnectToHost(string, int).

public bool IsSocketConnected()

Returns

bool

JoinMulticastGroup(string, string)

Joins the multicast group specified by multicastAddress using the interface identified by interfaceName.

You can join the same multicast group with multiple interfaces. Use GetLocalInterfaces() to know which are available.

Note: Some Android devices might require the CHANGE_WIFI_MULTICAST_STATE permission for multicast to work.

public Error JoinMulticastGroup(string multicastAddress, string interfaceName)

Parameters

multicastAddress string
interfaceName string

Returns

Error

LeaveMulticastGroup(string, string)

Removes the interface identified by interfaceName from the multicast group specified by multicastAddress.

public Error LeaveMulticastGroup(string multicastAddress, string interfaceName)

Parameters

multicastAddress string
interfaceName string

Returns

Error

SetBroadcastEnabled(bool)

Enable or disable sending of broadcast packets (e.g. set_dest_address("255.255.255.255", 4343). This option is disabled by default.

Note: Some Android devices might require the CHANGE_WIFI_MULTICAST_STATE permission and this option to be enabled to receive broadcast packets too.

public void SetBroadcastEnabled(bool enabled)

Parameters

enabled bool

SetDestAddress(string, int)

Sets the destination address and port for sending packets and variables. A hostname will be resolved using DNS if needed.

Note: SetBroadcastEnabled(bool) must be enabled before sending packets to a broadcast address (e.g. 255.255.255.255).

public Error SetDestAddress(string host, int port)

Parameters

host string
port int

Returns

Error

Wait()

Waits for a packet to arrive on the bound address. See Bind(int, string, int).

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:

var socket = new PacketPeerUdp();
  // Server
  socket.SetDestAddress("127.0.0.1", 789);
  socket.PutPacket("Time to stop".ToAsciiBuffer());

// Client while (socket.Wait() == OK) { string data = socket.GetPacket().GetStringFromASCII(); if (data == "Time to stop") { return; } }

public Error Wait()

Returns

Error