我正在尝试创建一个专为企业用户设计的p2p应用程序,该组织在不同的子网上有计算机集群。
发送数据包进行发现时客户端在不同的子网上无法找到彼此但是当客户端在同一个子网上时,他们能够按预期发现对方。我已经通过多播和广播测试了客户端。他们都有同样的问题。
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;
}