Is it possible to broadcast the server IP using Networking in Godot ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LeDernier

I just want my game clients to be able to connect at the server without having to enter manualy an IP.

Do you have any solution ? I am interested in any alternative if you have any other idea.

:bust_in_silhouette: Reply From: Christoffer Schindel

Well, if you don’t want the user to manually enter the IP, you could just hardcode it, I suppose. Or store the IP somewhere, and then fetch it automatically.

An example in C#:

enter image description here

I did this for now, but without configuring the network, I can’t know before wich IP the server will have. This is why I want to broadcast the server IP when it is setup.

LeDernier | 2018-09-10 16:43

And you can’t get a domain name or a hostname?

Ertain | 2018-09-10 23:06

No because I just want this to be local (LAN).

LeDernier | 2018-09-11 00:37

:bust_in_silhouette: Reply From: ArthyChaux

Hey,
I just did a LAN broadcast to do exactly what you need.

So what you need to define on both computers/devices is:

const UDP_BROADCAST_FREQUENCY: float # 3 for me
var udp_network: PacketPeerUDP
var server_broadcasting_udp_port: int # 6868 for me
var _broadcast_timer = 0

then for client, just put in _ready() :

udp_network = PacketPeerUDP.new()

if udp_network.listen(server_broadcasting_udp_port) != OK:
	print("Error listening on port: ", server_broadcasting_udp_port)
else:
	print("Listening on port: ", server_broadcasting_udp_port)

and in _process(delta):

if udp_network.get_available_packet_count() > 0:
    var array_bytes = udp_network.get_packet()
    var packet_string = array_bytes.get_string_from_ascii()

    var array = packet_string.split(",")
    var new_server_id = array[0]
    var new_server_name = array[1]
    var new_server_port = array[2]
    var new_server_players = array[3]
    var new_server_max_p = array[4]
    # Do want you want with it

and for the server, just do when initializing:

udp_network = PacketPeerUDP.new()
udp_network.set_broadcast_enabled(true)

and in _process(delta), put:

_broadcast_timer -= delta
if _broadcast_timer <= 0
    _broadcast_timer = UDP_BROADCAST_FREQUENCY
    var stg = server_id + "," +  server_name + "," + server_port + "," + player_number + "," + max_player_number
    var pac = stg.to_ascii()

    for address in IP.get_local_addresses():
	    var parts = address.split('.')
	    if (parts.size() == 4):
		    parts[3] = '255'
		    udp_network.set_dest_address(parts.join('.'), server_broadcasting_udp_port)
		    var error = udp_network.put_packet(pac)
		    if error == 1:
		        print("Error while sending to ", ip, ":", port)