- Joined
- Nov 22, 2011
- Messages
- 5,681
[highlight=vb]'
Imports System.Net
Public Class IPv4Helpers
Public Shared Function GetBroadcastAddress(ByVal ip As String, ByVal subnetMask As String) As String
'Convert the IPv4 address and subnet mask from Strings to IPAdress objects
Dim ip_subnetMask As IPAddress = IPAddress.Parse(subnetMask)
Dim ip_addr As IPAddress = IPAddress.Parse(ip)
'Use the subnet mask to get the subnet from the IP adress.
'Example: For the IP address 192.168.1.100 with a subnet mask of 255.255.255.0
'the subnet is 192.168.1.0
Dim subnet As UInteger = GetAddressBits(ip_subnetMask) And GetAddressBits(ip_addr)
'Invert the subnet mask. Example: 255.255.255.0 becomes 0.0.0.255
Dim invertedMask As UInteger = Not GetAddressBits(ip_subnetMask)
'Combine the inverted subnet mask with the subnet to get the broadcast address
'For example: The subnet 192.168.1.0 with an inverted mask of 0.0.0.255 becomes
'192.168.1.255
Return New IPAddress(BitConverter.GetBytes(invertedMask Or subnet)).ToString
End Function
Private Shared Function GetAddressBits(ByVal addr As IPAddress) As UInteger
Return BitConverter.ToUInt32(addr.GetAddressBytes, 0)
End Function
End Class
[/highlight]
The above is a simple class for calculating the broadcast address given an IPv4 IP address and a subnet mask. This is usually needed in Winsock applications that utilize UDP broadcasts as part of it's operation.
Here is an example of how to use it:-
[highlight=vb]'
Dim broadcastAddress As String = IPv4Helpers.GetBroadcastAddress("192.168.7.99", "255.255.255.0")
Debug.WriteLine(broadcastAddress)
[/highlight]
Which outputs the following:-
Imports System.Net
Public Class IPv4Helpers
Public Shared Function GetBroadcastAddress(ByVal ip As String, ByVal subnetMask As String) As String
'Convert the IPv4 address and subnet mask from Strings to IPAdress objects
Dim ip_subnetMask As IPAddress = IPAddress.Parse(subnetMask)
Dim ip_addr As IPAddress = IPAddress.Parse(ip)
'Use the subnet mask to get the subnet from the IP adress.
'Example: For the IP address 192.168.1.100 with a subnet mask of 255.255.255.0
'the subnet is 192.168.1.0
Dim subnet As UInteger = GetAddressBits(ip_subnetMask) And GetAddressBits(ip_addr)
'Invert the subnet mask. Example: 255.255.255.0 becomes 0.0.0.255
Dim invertedMask As UInteger = Not GetAddressBits(ip_subnetMask)
'Combine the inverted subnet mask with the subnet to get the broadcast address
'For example: The subnet 192.168.1.0 with an inverted mask of 0.0.0.255 becomes
'192.168.1.255
Return New IPAddress(BitConverter.GetBytes(invertedMask Or subnet)).ToString
End Function
Private Shared Function GetAddressBits(ByVal addr As IPAddress) As UInteger
Return BitConverter.ToUInt32(addr.GetAddressBytes, 0)
End Function
End Class
[/highlight]
The above is a simple class for calculating the broadcast address given an IPv4 IP address and a subnet mask. This is usually needed in Winsock applications that utilize UDP broadcasts as part of it's operation.
Here is an example of how to use it:-
[highlight=vb]'
Dim broadcastAddress As String = IPv4Helpers.GetBroadcastAddress("192.168.7.99", "255.255.255.0")
Debug.WriteLine(broadcastAddress)
[/highlight]
Which outputs the following:-
Code:
192.168.7.255