以下代码可以根据主机名获取IP,至于扫描局域网的主机名和ip,建议多去逛逛csdn。
Dim hostInfo As IPHostEntry
Dim address As NetIPAddress()
hostInfo = DnsGetHostEntry("ZHH") ‘ZHH即主机名
address = hostInfoAddressList
Dim index As Integer
For index = 0 To addressLength - 1
MsgBox(address(index)ToString)
Next index
Dim IPAdress As SystemNetIPAddress
HostName = SystemNetDnsGetHostName '获得本机的机器名
IPAdress = SystemNetDnsGetHostByName(HostName)AddressListGetValue(0) '获得本机的IP
MeTextBox1Text = HostName '本机机器名
MeTextBox2Text = IPAdressToString '本机的IP
Option Explicit
Private Const WSADESCRIPTION_LEN = 256
Private Const WSASYS_STATUS_LEN = 128
Private Type HOSTENT
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type
Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(WSADESCRIPTION_LEN) As Byte
szSystemStatus(WSASYS_STATUS_LEN) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Declare Function gethostbyname Lib "wsock32dll" (ByVal szName As String) As Long
Private Declare Function WSAStartup Lib "wsock32dll" (ByVal wVersionRequested As Integer, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "wsock32dll" () As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function LookupIPAddress(ByVal sHostName As String) As String
Dim wsa As WSADATA
Dim nRet As Long
Dim nTemp As Long
Dim bTemp(0 To 3) As Byte
Dim sOut As String
Dim he As HOSTENT
'Initialize WinSock
WSAStartup &H10, wsa
'Attempt to lookup the host
nRet = gethostbyname(sHostName)
'If it failed, just return nothing
If nRet = 0 Then
sOut = " "
Else
'Take a look at the resulting hostent structure
CopyMemory he, ByVal nRet, Len(he)
'Are there atlest four bytes, then we have
' at least one address
If heh_length >= 4 Then
'Copy the address out,
CopyMemory nTemp, ByVal heh_addr_list, 4
CopyMemory bTemp(0), ByVal nTemp, 4
' and format it
sOut = Format(bTemp(0)) & " " & Format(bTemp(1)) & " " _
& Format(bTemp(2)) & " " & Format(bTemp(3))
Else
sOut = " "
End If
End If
WSACleanup
LookupIPAddress = sOut
End Function
Private Sub Command1_Click()
MsgBox LookupIPAddress("localhost")
End Sub
修改下 API声明 测试代码没问题
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserName Lib "advapi32dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub GetName()
Dim sJM As String
Dim sMC As String
Dim sIP As String
Dim s As String
s = String(128, Chr(0))
GetComputerName s, Len(s)
s = UCase(Left(s, InStr(1, s, Chr(0)) - 1))
sJM = s'计算机名称
s = String(128, Chr(0))
GetUserName s, Len(s)
s = UCase(Left(s, InStr(1, s, Chr(0)) - 1))
sMC = s‘当前用户名称
End Sub
'用这种方法在远程获得再发回本机就可以吧
'取计算机型号暂时不会
以上就是关于vb.net socket 通过主机名而非IP连接局域网主机全部的内容,包括:vb.net socket 通过主机名而非IP连接局域网主机、请问在VB.NET中怎样获取本机IP、vb通过主机名获取ip等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)