2

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"?

Community
  • 1
  • 1
chris
  • 57
  • 1
  • 1
  • 9

3 Answers3

9

I know this is old, but I didn't like the loop, so here is one more solution:

    public static IPAddress GetBroadcastAddress(UnicastIPAddressInformation unicastAddress)
    {
       return GetBroadcastAddress(unicastAddress.Address, unicastAddress.IPv4Mask);
    }

    public static IPAddress GetBroadcastAddress(IPAddress address, IPAddress mask)
    {
        uint ipAddress = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
        uint ipMaskV4 = BitConverter.ToUInt32(mask.GetAddressBytes(), 0);
        uint broadCastIpAddress = ipAddress | ~ipMaskV4;

        return new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
    }
nb.alexiev
  • 379
  • 3
  • 10
  • This answer can be paired with this answer https://stackoverflow.com/a/4553625/4906348, to get a broadcast from all network interface on Machine – Benyamin Limanto Apr 30 '23 at 07:03
4
public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask)
{
  byte[] ipAdressBytes = address.GetAddressBytes();
  byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

  if (ipAdressBytes.Length != subnetMaskBytes.Length)
    throw new ArgumentException("Lengths of IP address and subnet mask do not match.");

  byte[] broadcastAddress = new byte[ipAdressBytes.Length];
  for (int i = 0; i < broadcastAddress.Length; i++)
  {
    broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255));
  }
  return new IPAddress(broadcastAddress);
}

Solution taken from here: http://blogs.msdn.com/b/knom/archive/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks.aspx

Flat Eric
  • 7,971
  • 9
  • 36
  • 45
  • 1
    thanks, but for this i need the subnetmask and this kind of the same problem. how can i get the Subnetmask for my IP? – chris Aug 13 '14 at 08:53
2
IPAddress GetBroadCastIP(IPAddress host, IPAddress mask)
    {
        byte[] broadcastIPBytes = new byte[4];
        byte[] hostBytes = host.GetAddressBytes();
        byte[] maskBytes = mask.GetAddressBytes();
        for (int i = 0; i < 4; i++)
        {
            broadcastIPBytes[i] = (byte)(hostBytes[i] | (byte)~maskBytes[i]);
        }
        return new IPAddress(broadcastIPBytes);
    }