Godot Version
Godot for windows 4.4.1 64 bit stable
Question
I cannot figure out how to get the godot class PacketPeerUDP to send or receive packets.
Link to the class: PacketPeerUDP — Godot Engine (stable) documentation in English
Here is my attempt to send a packet with netcat listening in udp mode on port 7000
extends Node
var udpSocket
func _ready() -> void:
print("HELLO")
udpSocket = PacketPeerUDP.new()
udpSocket.connect_to_host("127.0.0.1", 7000);
udpSocket.put_packet(PackedByteArray([42, 42, 42]))
and here are the results
I get similar results when listening for a packet, as the packet is never received
Keywords
UDP PacketPeerUDP
Check the error codes for the connect_to_host()
and put_packet()
calls. They may tell you something useful.
Thanks for your reply. The returned error codes are 0, which means ok. The code also does not seem to work on version 4.3
mrcdk
May 11, 2025, 4:33am
4
This works fine for me:
extends Node
var server:PacketPeerUDP
var client:PacketPeerUDP
func _ready() -> void:
server = PacketPeerUDP.new()
server.bind(53423)
client = PacketPeerUDP.new()
client.set_dest_address("localhost", 53423)
client.put_packet("Hello world".to_utf8_buffer())
await get_tree().create_timer(1).timeout
client.put_packet("Another packet".to_utf8_buffer())
func _process(delta: float) -> void:
if server.get_available_packet_count() > 0:
var packet = server.get_packet()
print(packet.get_string_from_utf8())
So I think your the problem is in the server side.
mrcdk:
var server:PacketPeerUDP
var client:PacketPeerUDP
func _ready() -> void:
server = PacketPeerUDP.new()
server.bind(53423)
client = PacketPeerUDP.new()
client.set_dest_address("localhost", 53423)
client.put_packet("Hello world".to_utf8_buffer())
await get_tree().create_timer(1).timeout
client.put_packet("Another packet".to_utf8_buffer())
func _process(delta: float) -> void:
if server.get_available_packet_count() > 0:
var packet = server.get_packet()
print(packet.get_string_from_utf8())
This code does work. However, I ran some tests with this UDP server udp-echo-server/server.py at master · mqp/udp-echo-server · GitHub , and it seems to work when i use netcat in udp mode, but when i run my godot code, it does not connect.
For some reason this code does not work on my windows computer, but the sockets work fine on the Debian machine I am using right now. I suspect that Godot might have been put on my firewall or something, I really have no idea
I think the error is occurring because i ran the server on a Linux terminal, and the Godot process was running on my windows machine. When the local server is on a windows terminal, everything works.
system
Closed
June 14, 2025, 3:47am
8
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.