I'm trying to make a game that is multiplayer using websockets. It has a dropdown list of machines running servers to connect to. I need IPs and machine names of every machine on the network to find out if any of them are hosting a server, and if so put there name in the dropdown. I've tried using various features of OS and also tried the IP singleton, but no luck. Could someone help me?

As this has been here a few days I'll have a 1am stab at it, noting I have done literally 0 networking in Godot (yet).

I need IPs and machine names of every machine on the network to find out if any of them are hosting a server, and if so put their name in the dropdown.

What you probably want to do is have each server broadcast its details on a regular interval, then each client just sniffs broadcast messages to see what's out there.

This may be a good place to start (second answer): https://godotengine.org/qa/32903/is-it-possible-broadcast-the-server-using-networking-godot

a month later

Hi Perodactyl,

Here's how I would have done it.

1 Send request to each ip of the network. 2 Server receive the request and send its hostname to the client. 3 The client receive the hostname and add it to a optionbutton. 4 Profit

For the client side:


extends Node
    
const PORT_SERVER = 1234
var clientUDP: PacketPeerUDP = PacketPeerUDP.new()


# Send to each IP of the Network "192.168.1.0" the message "Hi server!"
func _ready():
	yield(get_tree().create_timer(5),"timeout") # wait 5s just in case.
	for i in range(254):
		clientUDP.set_dest_address("192.168.1." + str(i), PORT_SERVER) # Define dest ip and port
		var msg = "Hi server!"
		var packet = msg.to_ascii() 
		clientUDP.put_packet(packet) # Send the massage to dest


# Check at each frame if received a udp packet at PORT_CLIENT (randomly choose by godot)
func _process(delta):
	if clientUDP.get_available_packet_count() > 0:
		var answer = clientUDP.get_packet()
		print("Client: ", answer.get_string_from_ascii()) # Get message from packet received from server
		# add Hostname receive to option menu..

For the server side:

extends Node

const PORT_SERVER = 1234
var srvUDP: PacketPeerUDP = PacketPeerUDP.new()

# Listen to PORT_SERVER
func _ready():
	if (srvUDP.listen(PORT_SERVER) != OK):
		print("Error listening to port: ", PORT_SERVER)
	else:
		print("Listening to port: " , PORT_SERVER)


# Check at each frame if received a udp packet at PORT_SERVER
func _process(delta):
	if srvUDP.get_available_packet_count() > 0:
		var answer = srvUDP.get_packet()
		var IP_CLIENT = srvUDP.get_packet_ip() # Get the ip of the client
		var PORT_CLIENT = srvUDP.get_packet_port() # Get the port of the client
		print("Server: " ,  answer.get_string_from_ascii())
		if answer.get_string_from_ascii() == "Hi server!": # If the message are the same, send its hostname to the client
			srvUDP.set_dest_address(IP_CLIENT, PORT_CLIENT) # Define the client as dest
			var msg = str(OS.get_environment("COMPUTERNAME")) # Get computer hostname
			var packet = msg.to_ascii()
			srvUDP.put_packet(packet) # Send computer hostname to client.

You can TCP instead of UDP.

Regards, Foreman21

a year later