0 votes

To send a UDP packet, do I just need this, or am I forgetting something?(Godot 3)

var txt_server = "34.209.96.220" 
var port = 1507
var socketUDP = PacketPeerUDP.new()

func _ready():
    start_client()

func _process(delta):
        socketUDP.set_dest_address(txt_server, port)
        socketUDP.put_var(["lalala"])

func start_client():
    if (socketUDP.listen(port, txt_server) != OK):
        printt("Error listening on port: " + str(port) + " in server: " + txt_server)
    else:
        printt("Listening on port: " + str(port) + " in server: " + txt_server)

func _exit_tree():
    socketUDP.close()
in Engine by (195 points)

2 Answers

+2 votes
Best answer

I leave here the code for a client and server using the "PacketPeerUDP" class:

client.gd

var IP_SERVER = "127.0.0.1" #"34.202.96.244"
var PORT_SERVER = 1507
var PORT_CLIENT = 1509
var socketUDP = PacketPeerUDP.new()

func _ready():
    start_client()

func _process(delta):
       if cron_send > 0:
        cron_send -= delta
        if cron_send <= 0:
            if socketUDP.is_listening():
                 socketUDP.set_dest_address(IP_SERVER, PORT_SERVER)
                 var stg = "hi server!"
                 var pac = stg.to_ascii()
                 socketUDP.put_packet(pac)
                 print("send!")
                 cron_send = 3

       if socketUDP.get_available_packet_count() > 0:
               var array_bytes = socketUDP.get_packet()
               printt("msg server: " + array_bytes.get_string_from_ascii())

func start_client():
    if (socketUDP.listen(PORT_CLIENT, IP_SERVER) != OK):
        printt("Error listening on port: " + str(PORT_CLIENT) + " in server: " + IP_SERVER)
    else:
        printt("Listening on port: " + str(PORT_CLIENT) + " in server: " + IP_SERVER)

func _exit_tree():
    socketUDP.close()

server.gd

var IP_CLIENT
var PORT_CLIENT
var PORT_SERVER = 1507
var IP_SERVER = "127.0.0.1"
var socketUDP = PacketPeerUDP.new()

func _ready():
    start_server()

func _process(delta):   

    if socketUDP.get_available_packet_count() > 0:
        var array_bytes = socketUDP.get_packet()
            var IP_CLIENT = get_packet_ip()
            var PORT_CLIENT = get_packet_port()
        printt("msg server: " + array_bytes.get_string_from_ascii())
            socketUDP.set_dest_address(IP_CLIENT, PORT_CLIENT)
            var stg = "hi server!"
            var pac = stg.to_ascii()
            socketUDP.put_packet(pac)

func start_server():
    if (socketUDP.listen(PORT_SERVER) != OK):
        printt("Error listening on port: " + str(PORT_SERVER))
    else:
        printt("Listening on port: " + str(PORT_SERVER))

func _exit_tree():
    socketUDP.close()
by (195 points)

I tried the code and it works perfectly for windows 10, but on android, there is no flow of data, any Idea why? I ruled out firewall by testing UDP using an app from playstore.

0 votes

Do you wish to send packet to multiple clients via a custom protocol?

If so, create a module that can hold a

http://docs.godotengine.org/en/latest/classes/class_networkedmultiplayerpeer.html?highlight=peer

which uses enet

if not, you should consider their simple high level networking

https://godotengine.org/article/godots-new-high-level-networking-preview

edit: forgot to link a demo project https://github.com/godotengine/godot-demo-projects/tree/master/networking/multiplayer_bomber

by (429 points)

I do not want to use "high level networking" (which only facilitates you if you have the server and client godot, does not work for me, which is an external server).

Also, I do not want to use enet of godot (which does not look very much like enet, it has no way to send messages through channels like enet does.

So I thank you for the answer, but you're too far from what you've been asked. I am trying to use a packetPeerUDP, but I do not know how to do it.

I am not sure what do you mean by channel? do you mean internet ports?

http://docs.godotengine.org/en/latest/classes/class_networkedmultiplayerenet.html#class-networkedmultiplayerenet

you can create a client and connect via port numbers.

oh ok, you want to create an external server with packetPeerUDP. I guess my answer is wrong.

look at the documentation of Enet, for some reason Godot decided to ignore the channels.http://enet.bespin.org/group__host.html#ga5567a95d7a45521dc9cba93a9066c940

I do not want to create an external server, I want to create the client with PacketPeerUDP. The server is in a language other than godot.

@hungrymonkey I'll see if Enet can handle it. Apparently @Faless take care of channels so that the blocking problem does not occur. https://github.com/godotengine/godot/pull/6657/commits/8b9e5453a57e873d43d748d9acf16be4b7a1aa19

I ended up not doing with the class "PacketPeerUDP" and I decided to translate a pure enet module to the godot. You can find it here: https://github.com/PerduGames/gdnet3

Hi! I need YouTube help. This module working in Android?
And for compile the module i put the folder gdnet3 into modules? because this folder contain others folder test a pnet .. Thanks foto answer

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.