vb.net 怎么获取客户端ip地址?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 怎么获取客户端ip地址?相关的知识,希望对你有一定的参考价值。
用了以下方法 都不行。
1.Request.ServerVariables("HTTP_X_FORWARDED_FOR") -注:获取到127.0.0.1
2.Request.ServerVariables("REMOTE_ADDR") -注:获取到127.0.0.1
3.Request.ServerVariables("LOCAL_ADDR") -注:获取到127.0.0.1
4. Dim ip As Net.IPAddress
Dim host As Net.IPHostEntry
host = Dns.GetHostByName(Dns.GetHostName)
ip = host.AddressList(0) - 注:获取的是服务器ip
有没有高手提供给我更好的方法?
如果你测试访问的时候,使用的是 localhost 进行访问的,那么服务器端获得的也就是127.0.0.1,因为localhost是一个环路地址,特殊的.如果你是通过你的ip地址进行访问的,那么获得的IP地址就会是正确的了. 要通过自己本地的IP进行访问,在控制面板的网络连接里找到本地连接,查看它的属性,并找到"详细信息"按钮,点击后弹出的对话框中IPV4地址就是本地地址.或打开命令行,输入 ipconfig /all 即可找到本地地址.在浏览器中通过这个IP地址访问,就不会是127.0.0.1了. 参考技术A Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Net.Sockets
Public Function GetIp() As String
Dim hostName As String = System.Net.Dns.GetHostName.ToString
Dim IPString As String = "127.0.0.1"
Dim ip As System.Net.IPAddress
For Each ip In System.Net.Dns.GetHostEntry(hostName).AddressList()
If ip.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
IPString = ip.ToString
Exit For
End If
Next
Return IPString
End Function
PS:我和你遇到的是同样的问题,之前还是可以返回正常的,装了一个虚拟机后就不行了,
这段函数返回,经过测试,是正确的。 参考技术B Dim strHostName As String strHostName = Dns.GetHostName() Dim ipEntry As IPHostEntry ipEntry = Dns.GetHostEntry(strHostName) Dim ip As String ip = ipEntry.AddressList(0).ToString()追问
这个获取的是本地的IP地址,我在浏览器上打开,则获取的是服务器的ip。
我需要在浏览器打开的时候,获取到的是我自己的IP.
vb.net 第十节 获取本机已探测的IP – 物理地址映射表 GetIpNetTable
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
Imports System.Runtime.InteropServices
Public Class Form11
'typedef struct _MIB_IPNETTABLE
' DWORD dwNumEntries;
' MIB_IPNETROW table[ANY_SIZE];
' MIB_IPNETTABLE, *PMIB_IPNETTABLE;
'typedef struct _MIB_IPNETROW
' DWORD dwIndex;
' DWORD dwPhysAddrLen;
' Byte bPhysAddr[MAXLEN_PHYSADDR];
' DWORD dwAddr;
' DWORD dwType;
' MIB_IPNETROW, *PMIB_IPNETROW;
'Type MIB_IPNETTABLE
' DwNumEntries As Long '当前包含MIB_IPNETROW类型的总数
' MIB_Table() As MIB_IPNETROW '指向一个包含MIB_IPNETROW类型的指针
'End Type
'Type MIB_IPNETROW
' dwIndex As Long '指定适配器的索引
' dwPhysAddrLen As Long 'bPhysAddrs字段内包含的物理接口的长度(字节),通常为6
' bPhysAddr(MAXLEN_PHYSADDR) As Byte '字节数组,包含适配器的物理地址
' dwAddr As Long 'IP地址
' dwType As Long 'ARP接口的类型,以下值之一:
' 常量名称 值 说明
' MIB_IPNET_TYPE_OTHER 1 其他条目
' MIB_IPNET_TYPE_INVALID 2 无效条目
' MIB_IPNET_TYPE_DYNAMIC 3 动态条目
' MIB_IPNET_TYPE_STATIC 4 静态条目
'End Type
Const MAXLEN_PHYSADDR = 7
<StructLayout(LayoutKind.Sequential)>
Public Structure MIB_IPNETTABLE
Public DwNumEntries As Long '当前包含MIB_IPNETROW类型的总数
Public MIB_Table() As MIB_IPNETROW '指向一个包含MIB_IPNETROW类型的指针
End Structure
<StructLayout(LayoutKind.Sequential)>
Public Structure MIB_IPNETROW
Public dwIndex As Integer '指定适配器的索引
Public dwPhysAddrLen As Integer 'bPhysAddrs字段内包含的物理接口的长度(字节),通常为6
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAXLEN_PHYSADDR)> Public bPhysAddr() As Byte '字节数组,包含适配器的物理地址
Public dwAddr As Integer 'IP地址
Public dwType As Integer 'ARP接口的类型,以下值之一:
End Structure
'DWORD GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder);
'Public Declare Function GetIpNetTable Lib "iphlpapi.dll" (pIpNetTable As MIB_IPNETTABLE, pdwSize As Long, bOrder As Long) As Long
Public Declare Function GetIpNetTable Lib "iphlpapi.dll" (ByRef pIpNetTable As IntPtr, ByRef pdwSize As Integer, ByVal bOrder As Integer) As Integer
Public Declare Function GetIpNetTable Lib "iphlpapi.dll" (ByVal pIpNetTable() As Byte, ByRef pdwSize As Integer, ByVal bOrder As Integer) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pIpNetTable As IntPtr = IntPtr.Zero
Dim pdwSize As Integer
Dim returnValue As Integer
'第一次调用,返回122:ERROR_INSUFFICIENT_BUFFER,缓冲区大小不足
'pdwSize参数返回实际需要的字节大小
returnValue = GetIpNetTable(pIpNetTable, pdwSize, True)
Dim bufIpNetTable(pdwSize - 1) As Byte
'分配内存
'ptrMibf = Marshal.AllocHGlobal(lenIft)
'第二次调用获得数据
returnValue = GetIpNetTable(bufIpNetTable, pdwSize, True)
Dim bytesNumEntries(3) As Byte
Array.Copy(bufIpNetTable, 0, bytesNumEntries, 0, 4)
Dim size As Integer = Marshal.SizeOf(bytesNumEntries(0)) * bytesNumEntries.Length
Dim ptrNumEntries As IntPtr = Marshal.AllocHGlobal(size)
Marshal.Copy(bytesNumEntries, 0, ptrNumEntries, 4)
Dim NumEntries As Integer
NumEntries = Marshal.ReadInt32(ptrNumEntries)
Dim MIBIPNETROW As New MIB_IPNETROW
Dim sizeMIBIPNETROW As Integer = Marshal.SizeOf(MIBIPNETROW)
For i As Integer = 0 To NumEntries - 1
Dim ptrMIBIPNETROW As New IntPtr
ptrMIBIPNETROW = Marshal.AllocHGlobal(sizeMIBIPNETROW)
Marshal.Copy(bufIpNetTable, 4 + sizeMIBIPNETROW * i, ptrMIBIPNETROW, sizeMIBIPNETROW)
MIBIPNETROW = Marshal.PtrToStructure(ptrMIBIPNETROW, GetType(MIB_IPNETROW))
TextBox1.Text &= "No." & i & vbCrLf
TextBox1.Text &= IntToIP(MIBIPNETROW.dwAddr) & vbCrLf
TextBox1.Text &= BitConverter.ToString(MIBIPNETROW.bPhysAddr, 0, 6) & vbCrLf
Next
End Sub
'Integer型IP转字符串
Private Function IntToIP(ByVal intAddr As Integer) As String
Dim bteIp() As Byte
bteIp = BitConverter.GetBytes(intAddr)
Dim ip As String = ""
For i As Integer = 0 To bteIp.Length - 1
ip &= bteIp(i).ToString & "."
Next
Return ip.Substring(0, ip.Length - 1)
End Function
End Class
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。
学习更多vb.net知识,请参看 vb.net 教程 目录
以上是关于vb.net 怎么获取客户端ip地址?的主要内容,如果未能解决你的问题,请参考以下文章