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