我正在编写一个应用程序来存储请求来自的 IP 地址。这些 IP 地址将作为 a 存储varbinary(16)
在我的数据库中。很明显,avarbinary(16)
的大小不合适。目前,我正在使用以下代码将请求的 IP 地址转换为 byte[];
HttpRequestMessage request = GetRequestMessage();
string ipAddress = string.Empty;
if (request.Properties.ContainsKey("MS_HttpContext"))
{
ipAddress = ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty property = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
ipAddress = property.Address;
}
byte[] bytes = new byte[ipAddress.Length * sizeof(char)];
System.Buffer.BlockCopy(ipAddress.ToCharArray(), 0, bytes, 0, bytes.Length);
// What is the maximum size of bytes?
我注意到字节的长度为 26。我的问题是,如果我需要支持 IPv6 地址,字节的最大大小是多少?我需要知道将我的大小更改为varbinary(16)
适当的长度。
谢谢!