I am trying to create a p2p application designed for corporate use,The organization has clusters of computers on different subnets.
When sending the packets for discovery The clients are unable to find eachother when on different subnets however when the clients are on the same subnet they are able to discover each other as intended.I have tested the clients both with multicasting and broadcasting and they both have the same issue.
private static UdpClient client = new UdpClient();
private static IPAddress multicastaddress = IPAddress.Parse("239.0.0.222");
private static IPEndPoint remoteep;
private static bool Running = false;
public static List<string> Discovery = new List<string> { };
public static void StartSend()
{
client.JoinMulticastGroup(multicastaddress);
remoteep = new IPEndPoint(multicastaddress, 2222);
byte[] Data = ASCIIEncoding.ASCII.GetBytes(Ipv4Address().ToString());
Running = true;
while (Running)
{
client.Send(Data, Data.Length, remoteep);
Console.WriteLine("Sending buffer");
Thread.Sleep(2000);
}
}
public static void StartRecieve()
{
client.ExclusiveAddressUse = false;
IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 2222);
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.Client.Bind(localEp);
client.JoinMulticastGroup(multicastaddress);
Running = true;
while (true)
{
Byte[] data = client.Receive(ref localEp);
string strData = ASCIIEncoding.ASCII.GetString(data);
if(!Discovery.Contains(strData))
{
Console.WriteLine("Found server running at "+ strData);
Discovery.Add(strData);
}
}
}
public static IPAddress Ipv4Address()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ReturnIP = IPAddress.Parse("0.0.0.0");
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ReturnIP = ip;
}
}
return ReturnIP;
}