本文整理汇总了C#中IPAddress.GetAddressBytes方法的典型用法代码示例。如果您正苦于以下问题:C# IPAddress.GetAddressBytes方法的具体用法?C# IPAddress.GetAddressBytes怎么用?C# IPAddress.GetAddressBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPAddress
的用法示例。
在下文中一共展示了IPAddress.GetAddressBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertIPAddress
internal static java.net.InetAddress ConvertIPAddress(IPAddress address, string hostname)
{
if (address.IsIPv6LinkLocal || address.IsIPv6SiteLocal)
{
return java.net.Inet6Address.getByAddress(hostname, address.GetAddressBytes(), (int)address.ScopeId);
}
else
{
return java.net.InetAddress.getByAddress(hostname, address.GetAddressBytes());
}
}
示例2: getBroadcastIP
static IPAddress getBroadcastIP(IPAddress pIP, IPAddress pIPv4Mask)
{
var lBroadcast = or(pIP.GetAddressBytes(), tilde(pIPv4Mask.GetAddressBytes()));
if (lBroadcast == null)
{
throw new Exception("pByte1.Length!=pByte2.Length.IP:" + pIP
+ " IPv4Mask:" + pIPv4Mask);
}
return new IPAddress(lBroadcast);
}
示例3: IsMulticast
private static bool IsMulticast(IPAddress address)
{
if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
return address.IsIPv6Multicast;
}
else
{
byte firstByte = address.GetAddressBytes()[0];
return firstByte >= 224 && firstByte <= 239;
}
}
示例4: GetArpaUrl
public static string GetArpaUrl(IPAddress address)
{
if (address == null)
{
throw new ArgumentNullException("address");
}
var result = String.Format(
CultureInfo.InvariantCulture,
"{0}.in-addr.arpa.",
String.Join(".", address.GetAddressBytes().Reverse()));
return result;
}
示例5: GetNetworkAddress
/// <summary>
///
/// </summary>
/// <param name="address"></param>
/// <param name="subnetMask"></param>
/// <returns></returns>
public static IPAddress GetNetworkAddress(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]));
return new IPAddress(broadcastAddress);
}
示例6: GetIPv4NetworkBroadcastAddress
/// <summary>
/// Returns the network broadcast address for the provided local IP address
/// </summary>
/// <param name="localIPAddress"></param>
/// <returns></returns>
public static IPAddress GetIPv4NetworkBroadcastAddress(IPAddress localIPAddress)
{
if (localIPAddress.AddressFamily != AddressFamily.InterNetwork)
throw new ArgumentException("The method is for IPv4 addresses only.");
#if WINDOWS_PHONE || NETFX_CORE
throw new NotImplementedException("This method has not yet been implemented for WP8 and WinRT.");
#else
//Determine the correct subnet address
//We initialise using a standard class C network subnet mask
IPAddress subnetAddress = new IPAddress(new byte[] { 255, 255, 255, 0});
#region Determine Correct SubnetMask
try
{
//Look at all possible addresses
foreach (var iFace in NetworkInterface.GetAllNetworkInterfaces())
{
var unicastAddresses = iFace.GetIPProperties().UnicastAddresses;
foreach (var address in unicastAddresses)
{
if (address.Address.Equals(localIPAddress))
{
subnetAddress = address.IPv4Mask;
//Use a go to to efficiently exist these loops
goto Exit;
}
}
}
}
catch (Exception)
{
//Ignore the exception and continue assuming a standard class C network
subnetAddress = new IPAddress(new byte[] { 255, 255, 255, 0 });
}
Exit:
#endregion
byte[] broadcastBytes = localIPAddress.GetAddressBytes();
byte[] subnetBytes = subnetAddress.GetAddressBytes();
for (int i = 0; i < broadcastBytes.Length; i++)
broadcastBytes[i] = (byte)(broadcastBytes[i] | ~subnetBytes[i]);
return new IPAddress(broadcastBytes);
#endif
}
示例7: ExpandIpRange
public IPAddress[] ExpandIpRange(IPAddress RangeFirst, IPAddress RangeLast)
{
/*Сворачиваем байты адреса в целое число*/
var start_ip = (uint)(CompressByteArray(RangeFirst.GetAddressBytes()));
/*Расчитываем дельту адресов(можно простым вычитанием, но xor логичнее и быстрее)*/
var delta = (uint)(CompressByteArray(RangeLast.GetAddressBytes()) ^ start_ip);
IPAddress[] retval = new IPAddress[delta];
/*Создаем список адресов диапазона
перебор осуществляется простым приращением*/
for (uint i = 0; i <= delta - 1; i++)
/*Разворачиваем число в массив байтов
Создаем экземпляр Ip адреса и передаем массив в конструктор
Добавляем адрес в список*/
try { retval[i] = new IPAddress(DecompressByteArray((int)(start_ip + i))); }
catch { continue; }
return retval;
}
示例8: Ping
public static int Ping(IPAddress IP)
{
IntPtr ICMPHandle;
Int32 iIP;
String sData;
ICMP_OPTIONS oICMPOptions = new ICMP_OPTIONS();
ICMP_ECHO_REPLY ICMPReply = new ICMP_ECHO_REPLY();
Int32 iReplies;
ICMPHandle = IcmpCreateFile();
iIP = BitConverter.ToInt32(IP.GetAddressBytes(), 0);
sData = "x";
oICMPOptions.Ttl = 255;
iReplies = IcmpSendEcho(ICMPHandle, iIP,
sData, sData.Length, ref oICMPOptions, ref ICMPReply,
Marshal.SizeOf(ICMPReply), 30);
IcmpCloseHandle(ICMPHandle);
return iReplies;
}
示例9: Ctor_Long_Success
public static void Ctor_Long_Success()
{
IPAddress ip = new IPAddress(0x2414188f);
Assert.Equal(BitConverter.GetBytes(0x2414188f), ip.GetAddressBytes());
}
示例10: Ctor_BytesScopeId_Success
public static void Ctor_BytesScopeId_Success(byte[] address, long scopeId)
{
IPAddress ip = new IPAddress(address, scopeId);
Assert.Equal(address, ip.GetAddressBytes());
Assert.Equal(scopeId, ip.ScopeId);
Assert.Equal(AddressFamily.InterNetworkV6, ip.AddressFamily);
}
示例11: SocketAddress
internal SocketAddress(IPAddress ipAddress)
: this((SockCel.AddressFamily)((int)ipAddress.AddressFamily), ((int)ipAddress.AddressFamily == (int)AddressFamily.InterNetwork) ? 16 : 28)
{
this.m_Buffer[2] = 0;
this.m_Buffer[3] = 0;
if ((int)ipAddress.AddressFamily == (int)AddressFamily.InterNetworkV6)
{
this.m_Buffer[4] = 0;
this.m_Buffer[5] = 0;
this.m_Buffer[6] = 0;
this.m_Buffer[7] = 0;
long scopeId = ipAddress.ScopeId;
this.m_Buffer[24] = (byte)scopeId;
this.m_Buffer[25] = (byte)(scopeId >> 8);
this.m_Buffer[26] = (byte)(scopeId >> 16);
this.m_Buffer[27] = (byte)(scopeId >> 24);
byte[] addressBytes = ipAddress.GetAddressBytes();
for (int i = 0; i < addressBytes.Length; i++)
{
this.m_Buffer[8 + i] = addressBytes[i];
}
return;
}
this.m_Buffer[4] = (byte)ipAddress.m_Address;
this.m_Buffer[5] = (byte)(ipAddress.m_Address >> 8);
this.m_Buffer[6] = (byte)(ipAddress.m_Address >> 16);
this.m_Buffer[7] = (byte)(ipAddress.m_Address >> 24);
}
示例12: Ctor_Long_Success
public static void Ctor_Long_Success(long address, byte[] expectedBytes)
{
IPAddress ip = new IPAddress(address);
Assert.Equal(expectedBytes, ip.GetAddressBytes());
Assert.Equal(AddressFamily.InterNetwork, ip.AddressFamily);
}
示例13: Ctor_Bytes_Success
public static void Ctor_Bytes_Success(byte[] address, AddressFamily expectedFamily)
{
IPAddress ip = new IPAddress(address);
Assert.Equal(address, ip.GetAddressBytes());
Assert.Equal(expectedFamily, ip.AddressFamily);
}
示例14: IsPrivateIP
private bool IsPrivateIP(IPAddress myIPAddress)
{
if (IPAddress.IsLoopback(myIPAddress)) return true;
if (myIPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
byte[] ipBytes = myIPAddress.GetAddressBytes();
// 10.0.0.0/24
if (ipBytes[0] == 10)
{
return true;
}
// 172.16.0.0/16
else if (ipBytes[0] == 172 && ipBytes[1] == 16)
{
return true;
}
// 192.168.0.0/16
else if (ipBytes[0] == 192 && ipBytes[1] == 168)
{
return true;
}
// 169.254.0.0/16
else if (ipBytes[0] == 169 && ipBytes[1] == 254)
{
return true;
}
}
return false;
}
示例15: SendAsync
private void SendAsync(IPAddress address, int timeout, byte[] buffer, PingOptions options, object userToken)
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
if (buffer.Length > MaxBufferSize)
{
throw new ArgumentException(SR.net_invalidPingBufferSize, "buffer");
}
if (timeout < 0)
{
throw new ArgumentOutOfRangeException("timeout");
}
if (address == null)
{
throw new ArgumentNullException("address");
}
// Check if address family is installed.
TestIsIpSupported(address);
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
{
throw new ArgumentException(SR.net_invalid_ip_addr, "address");
}
// FxCop: need to snapshot the address here, so we're sure that it's not changed between the permission
// check and the operation, and to be sure that IPAddress.ToString() is called and not some override.
IPAddress addressSnapshot;
if (address.AddressFamily == AddressFamily.InterNetwork)
{
addressSnapshot = new IPAddress(address.GetAddressBytes());
}
else
{
addressSnapshot = new IPAddress(address.GetAddressBytes(), address.ScopeId);
}
CheckStart(true);
try
{
_cancelled = false;
_asyncOp = AsyncOperationManager.CreateOperation(userToken);
InternalSend(addressSnapshot, buffer, timeout, options, true);
}
catch (Exception e)
{
Finish(true);
throw new PingException(SR.net_ping, e);
}
}