Android 11 Won't Receive Packets sent to multicast Address

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

I’m attempting to add a simple lobby that works over LAN to my Godot Project. I found
this GitHub example https://github.com/Wavesonics/LANServerBroadcast
and have been trying to get it to work on Android. I have enabled ChangeWifiMulticast and Internet permissions. The example works fine on Android 10 devices but my Pixel 4a Android 11 device is not able to receive any packets when discovering and get_available_packet_count() returns 0. Android 11 is able to Host but is not able to discover. No errors are seen over logcat. Does anyone know a workaround to get this working on Android 11 to receive multicast packets?

The main classes are as follows:
ServerAdvertiser (works in Android 11)

extends Node
class_name ServerAdvertiser

const DEFAULT_PORT := 3111

# How often to broadcast out to the network that this host is active
export (float) var broadcast_interval: float = 1.0
var serverInfo := {"name": "LAN Game"}

var socketUDP: PacketPeerUDP
var broadcastTimer := Timer.new()
var broadcastPort := DEFAULT_PORT

func _enter_tree():
	broadcastTimer.wait_time = broadcast_interval
	broadcastTimer.one_shot = false
	broadcastTimer.autostart = true
	
	if get_tree().is_network_server():
		add_child(broadcastTimer)
		broadcastTimer.connect("timeout", self, "broadcast") 
		
		socketUDP = PacketPeerUDP.new()
		socketUDP.set_broadcast_enabled(true)
		socketUDP.set_dest_address('255.255.255.255', broadcastPort)

func broadcast():
	#print('Broadcasting game...')
	var packetMessage := to_json(serverInfo)
	var packet := packetMessage.to_ascii()
	socketUDP.put_packet(packet)

func _exit_tree():
	broadcastTimer.stop()
	if socketUDP != null:
		socketUDP.close()

ServerListener(does not work in Android 11)

  extends Node
    class_name ServerListener
    
    signal new_server
    signal remove_server
    
    var cleanUpTimer := Timer.new()
    var socketUDP := PacketPeerUDP.new()
    var listenPort := ServerAdvertiser.DEFAULT_PORT
    var knownServers = {}
    
    # Number of seconds to wait when a server hasn't been heard from
    # before calling remove_server
    export (int) var server_cleanup_threshold: int = 3
    
    func _init():
    	cleanUpTimer.wait_time = server_cleanup_threshold
    	cleanUpTimer.one_shot = false
    	cleanUpTimer.autostart = true
    	cleanUpTimer.connect("timeout", self, 'clean_up')
    	add_child(cleanUpTimer)
    
    func _ready():
    	knownServers.clear()
    	
    	if socketUDP.listen(listenPort) != OK:
    		print("GameServer LAN service: Error listening on port: " + str(listenPort))
    	else:
    		print("GameServer LAN service: Listening on port: " + str(listenPort))
    
    func _process(delta):
    	if socketUDP.get_available_packet_count() > 0: #RETURNS 0 in Android 11
    		var serverIp = socketUDP.get_packet_ip()
    		var serverPort = socketUDP.get_packet_port()
    		var array_bytes = socketUDP.get_packet()
    		
    		if serverIp != '' and serverPort > 0:
    			# We've discovered a new server! Add it to the list and let people know
    			if not knownServers.has(serverIp):
    				var serverMessage = array_bytes.get_string_from_ascii()
    				var gameInfo = parse_json(serverMessage)
    				gameInfo.ip = serverIp
    				gameInfo.lastSeen = OS.get_unix_time()
    				knownServers[serverIp] = gameInfo
    				print("New server found: %s - %s:%s" % [gameInfo.name, gameInfo.ip, gameInfo.port])
    				emit_signal("new_server", gameInfo)
    			# Update the last seen time
    			else:
    				var gameInfo = knownServers[serverIp]
    				gameInfo.lastSeen = OS.get_unix_time()
    
    func clean_up():
    	var now = OS.get_unix_time()
    	for serverIp in knownServers:
    		var serverInfo = knownServers[serverIp]
    		if (now - serverInfo.lastSeen) > server_cleanup_threshold:
    			knownServers.erase(serverIp)
    			print('Remove old server: %s' % serverIp)
    			emit_signal("remove_server", serverIp)
    
    func _exit_tree():
    	socketUDP.close()
:bust_in_silhouette: Reply From: powerpoint45

I was able to get it to work by setting socketUDP.set_broadcast_enabled (true) in _ready of ServerListener

:bust_in_silhouette: Reply From: PranshuTG

it works in older versions of godot

*Used to
i just tried again and it failed

PranshuTG | 2021-08-03 07:16