From 1043b2c541a9cfb6761500ca5472b6afa670fb8f Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 3 Nov 2024 22:50:40 +0100 Subject: [PATCH] Use default network interface --- Assets/Scripts/ServiceDiscovery.cs | 59 +++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/ServiceDiscovery.cs b/Assets/Scripts/ServiceDiscovery.cs index 805608b..39c550a 100644 --- a/Assets/Scripts/ServiceDiscovery.cs +++ b/Assets/Scripts/ServiceDiscovery.cs @@ -1,6 +1,7 @@ using System; using System.Net; using System.Net.Sockets; +using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; using System.Threading; @@ -17,19 +18,73 @@ public class ServiceDiscovery : MonoBehaviour private string receivedPath; private string receivedHost; + private IPAddress defaultIP; + private const string multicastAddress = "224.0.0.251"; private const int multicastPort = 5353; private Queue serviceQueue = new Queue(); + private IPAddress GetDefaultInterfaceIP() + { + foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) + { + if (ni.OperationalStatus == OperationalStatus.Up) + { + var ipProps = ni.GetIPProperties(); + if (ipProps.GatewayAddresses.Count > 0) + { + foreach (UnicastIPAddressInformation ip in ipProps.UnicastAddresses) + { + if (ip.Address.AddressFamily == AddressFamily.InterNetwork) + { + return ip.Address; + } + } + } + } + } + + return null; + } + + private List GetRoutableLocalIPs() + { + List localIPs = new List(); + + foreach (IPAddress local in Dns.GetHostEntry(Dns.GetHostName()).AddressList) + { + if (local.AddressFamily == AddressFamily.InterNetwork) + { + byte[] bytes = local.GetAddressBytes(); + if (bytes[0] == 169 && bytes[1] == 254) + { + Debug.Log($"Skipping non-routable address: {local}"); + continue; + } + + localIPs.Add(local); + } + } + + return localIPs; + } + public void StartListening(Action action) { try { + defaultIP = GetDefaultInterfaceIP(); + if (defaultIP == null) + { + Debug.LogError("No default interface found. Cannot start multicast listener."); + return; + } + udpClient = new UdpClient(); udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, multicastPort)); - udpClient.JoinMulticastGroup(IPAddress.Parse(multicastAddress)); + udpClient.Client.Bind(new IPEndPoint(defaultIP, multicastPort)); + udpClient.JoinMulticastGroup(IPAddress.Parse(multicastAddress), defaultIP); this.action = action;