My first Problem was, C# UDP Chat receive no message, one atempt to fix this was to avoid.
IPAddress.Broadcast
So i wrote a function to determine the local broadcast:
private IPAddress get_broadcast()
{
try
{
string ipadress;
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); // get a list of all local IPs
IPAddress localIpAddress = ipHostInfo.AddressList[0]; // choose the first of the list
ipadress = Convert.ToString(localIpAddress); // convert to string
ipadress = ipadress.Substring(0, ipadress.LastIndexOf(".")+1); // cuts of the last octet of the given IP
ipadress += "255"; // adds 255 witch represents the local broadcast
return IPAddress.Parse(ipadress);
}
catch (Exception e)
{
errorHandler(e);
return IPAddress.Parse("127.0.0.1");// in case of error return the local loopback
}
}
but this only works on /24 Networks I often switch between /24(at home) and /16(at work) networks. So a hard coded subnetmask don't fit my requirements.
so, is there any good way to determine the local broadcast without using "IPAddress.Broadcast"?