Question about LAN multiplayer

Godot Version

4.2

Question

I was following a tutorial about making a LAN server browser. And it works in my machine.

The tutorial uses my machine’s IP to broadcast itself to others so they can join.

@export var defaultIp:String = "192.168.///.255" #(not sure if it is safe to post my ip on-line lol)

...

func _on_broad_cast_timeout():
	print("BroadCasting!")
	var data = JSON.stringify(MultiplayerManager.roomProperties)
	var packet = data.to_ascii_buffer()
	
	roomInfoSender.put_packet(packet)

My question is: Will there be any problems if other machines have different ips than mine? Some routers could have different ips than mine and it would be impossible to find them in the server list, since it’s only broadcasting for ips in that specific range.
If so, can I adapt the ip string to get the ip range of the user to broadcast it correctly?
Thanks for reading.

You can broadcast UDP packets to anyone listening by binding to 255.255.255.255 and setting broadcast mode. Here’s my lan broadcaster scripts, based on this plugin: Github LANServerBroadcaster

# Sender
extends Node

var socket := PacketPeerUDP.new()
const lan_broadcast_port := 42355
@export var broadcast_message: String = "BlippyServer"

func _ready() -> void:
	socket.set_broadcast_enabled(true)
	var err := socket.set_dest_address("255.255.255.255", lan_broadcast_port)
	assert(err == OK, "Couldn't set broadcasting")

	var timer := Timer.new()
	timer.autostart = true
	timer.wait_time = 1.5
	add_child(timer)
	timer.timeout.connect(broadcast)

func broadcast() -> void:
	var packet := broadcast_message.to_ascii_buffer()
	socket.put_packet(packet)
# Reciever
extends Node

signal new_server(ip: String, message: String)

var socket := PacketPeerUDP.new()
const lan_broadcast_port := 42355
var found_servers: Array[String] = []

func _ready() -> void:
	var bind_err := socket.bind(lan_broadcast_port)
	if bind_err == OK:
		var timer := Timer.new()
		timer.autostart = true
		add_child(timer)
		timer.timeout.connect(check_packets)
	else:
		push_warning("Couldn't bind lan broadcast listener")


func check_packets() -> void:
	while socket.get_available_packet_count() > 0:
		var serverip := socket.get_packet_ip()
		var port := socket.get_packet_port()
		var bytes := socket.get_packet()
		var message := bytes.get_string_from_ascii()

		if serverip != '' and port > 0:
			if not serverip in found_servers:
				new_server.emit(serverip, message)
				found_servers.append(serverip)

Ah, sorry for dumping all of this, I am reading when broadcasting 255 is used as a subnet mask.

192.168.1.255 would broadcast to any ips in the range 192.168.1.[0-255]

192.168.255.255 would broadcast to this range 192.168.[0-255].[0-255]

And so on.

I would recommend at least doing 192.168.255.255, this will support most home networks, I personally lease my local IPs with 192.168.88.[0-255], class A networks, like those in a large office might use 10.[0-255].[0-255].[0-255] so if you want to support after-hours LAN parties that would be nice.

There isn’t much harm in 255.255.255.255, firewalls exist and all that, your game’s packets wont go far.

Ooh, thanks for the reply! I think I will default it to 192.168.255.255 and then have an option to change it, just in case I need it when showing it to my friends. Thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.