Index · Object → RefCounted → UDPServer

UDPServer

Helper class to implement a UDP server.

Class Description

A simple server that opens a UDP socket and returns connected PacketPeerUDP upon receiving new packets. See also PacketPeerUDP.connect_to_host().

After starting the server (listen()), you will need to poll() it at regular intervals (e.g. inside Node._process()) for it to process new packets, delivering them to the appropriate PacketPeerUDP, and taking new connections.

Below a small example of how it can be used:

# server_node.gd
class_name ServerNode
extends Node

var server = UDPServer.new()
var peers = []

func _ready():
	server.listen(4242)

func _process(delta):
	server.poll() # Important!
	if server.is_connection_available():
		var peer = server.take_connection()
		var packet = peer.get_packet()
		print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()])
		print("Received data: %s" % [packet.get_string_from_utf8()])
		# Reply so it knows we received the message.
		peer.put_packet(packet)
		# Keep a reference so we can keep contacting the remote peer.
		peers.append(peer)

	for i in range(0, peers.size()):
		pass # Do something with the connected peers.
# client_node.gd
class_name ClientNode
extends Node

var udp = PacketPeerUDP.new()
var connected = false

func _ready():
	udp.connect_to_host("127.0.0.1", 4242)

func _process(delta):
	if !connected:
		# Try to contact server
		udp.put_packet("The answer is... 42!".to_utf8_buffer())
	if udp.get_available_packet_count() > 0:
		print("Connected: %s" % udp.get_packet().get_string_from_utf8())
		connected = true

Properties

var max_pending_connections: int = 16

Define the maximum number of pending connections, during poll(), any new pending connection exceeding that value will be automatically dropped. Setting this value to 0 effectively prevents any new pending connection to be accepted (e.g. when all your players have connected).

Methods

func get_local_port() -> int

Returns the local port this server is listening to.

func is_connection_available() -> bool

Returns true if a packet with a new address/port combination was received on the socket.

func is_listening() -> bool

Returns true if the socket is open and listening on a port.

func listen(port: int, bind_address: String = "*") -> Error

Starts the server by opening a UDP socket listening on the given port. You can optionally specify a bind_address to only listen for packets sent to that address. See also PacketPeerUDP.bind().

func poll() -> Error

Call this method at regular intervals (e.g. inside Node._process()) to process new packets. Any packet from a known address/port pair will be delivered to the appropriate PacketPeerUDP, while any packet received from an unknown address/port pair will be added as a pending connection (see is_connection_available() and take_connection()). The maximum number of pending connections is defined via max_pending_connections.

func stop() -> void

Stops the server, closing the UDP socket if open. Will close all connected PacketPeerUDP accepted via take_connection() (remote peers will not be notified).

func take_connection() -> PacketPeerUDP

Returns the first pending connection (connected to the appropriate address/port). Will return null if no new connection is available. See also is_connection_available(), PacketPeerUDP.connect_to_host().

Inherited from RefCounted (4)
func init_ref() -> bool
func reference() -> bool
func unreference() -> bool
Inherited from Object (62)
func _get(property: StringName) -> Variant
func _get_property_list() -> Dictionary[]
func _init() -> void
func _iter_get(iter: Variant) -> Variant
func _iter_init(iter: Array) -> bool
func _iter_next(iter: Array) -> bool
func _notification(what: int) -> void
func _set(property: StringName, value: Variant) -> bool
func _to_string() -> String
func _validate_property(property: Dictionary) -> void
func add_user_signal(signal: String, arguments: Array = []) -> void
func call(method: StringName) -> Variant
func callv(method: StringName, arg_array: Array) -> Variant
func cancel_free() -> void
func connect(signal: StringName, callable: Callable, flags: int = 0) -> Error
func disconnect(signal: StringName, callable: Callable) -> void
func emit_signal(signal: StringName) -> Error
func free() -> void
func get(property: StringName) -> Variant
func get_class() -> String
func get_incoming_connections() -> Dictionary[]
func get_indexed(property_path: NodePath) -> Variant
func get_meta(name: StringName, default: Variant = null) -> Variant
func get_meta_list() -> StringName[]
func get_method_list() -> Dictionary[]
func get_property_list() -> Dictionary[]
func get_signal_connection_list(signal: StringName) -> Dictionary[]
func get_signal_list() -> Dictionary[]
func has_meta(name: StringName) -> bool
func has_method(method: StringName) -> bool
func has_signal(signal: StringName) -> bool
func is_class(class: StringName) -> bool
func is_connected(signal: StringName, callable: Callable) -> bool
func notification(what: int, reversed: bool = false) -> void
func remove_meta(name: StringName) -> void
func remove_user_signal(signal: StringName) -> void
func set(property: StringName, value: Variant) -> void
func set_block_signals(enable: bool) -> void
func set_deferred(property: StringName, value: Variant) -> void
func set_indexed(property_path: NodePath, value: Variant) -> void
func set_message_translation(enable: bool) -> void
func set_meta(name: StringName, value: Variant) -> void
func set_script(script: Variant) -> void
func set_translation_domain(domain: StringName) -> void
func to_string() -> String
func tr(message: StringName, context: StringName = &"") -> String
func tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") -> String

Signals

Inherited from Object (2)