知道局域网中其他计算机的IP,如何得到它的计算机名?

dongdong 2000-09-04 12:11:00
问题有两个,会一些解答一些,谢谢了。
1。如何枚举局域网中的计算机;
2。如果已经知道某计算机的IP,如何得到它的计算机名;
...全文
316 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongdong 2000-09-06
  • 打赏
  • 举报
回复
thanks,Chen_lin.
Chen_Lin 2000-09-06
  • 打赏
  • 举报
回复
------知道某计算机的IP,得到它的计算机名----------
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128
Private Const WS_VERSION_REQD As Long = &H101
Private Const IP_SUCCESS As Long = 0
Private Const SOCKET_ERROR As Long = -1
Private Const AF_INET = 2

Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To WSADescription_Len) As Byte
szSystemStatus(0 To WSASYS_Status_Len) As Byte
imaxsockets As Integer
imaxudp As Integer
lpszvenderinfo As Long
End Type

Private Declare Function WSAStartup Lib "wsock32.dll" _
(ByVal VersionReq As Long, _
WSADataReturn As WSADATA) As Long

Private Declare Function WSACleanup Lib "wsock32.dll" () As Long

Private Declare Function inet_addr Lib "wsock32.dll" _
(ByVal s As String) As Long

Private Declare Function gethostbyaddr Lib "wsock32.dll" _
(haddr As Long, _
ByVal hnlen As Long, _
ByVal addrtype As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(xDest As Any, _
xSource As Any, _
ByVal nbytes As Long)

Private Declare Function lstrlen Lib "kernel32" _
Alias "lstrlenA" _
(lpString As Any) As Long


Public Function SocketsInitialize() As Boolean

Dim WSAD As WSADATA

SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS

End Function


Public Sub SocketsCleanup()

If WSACleanup() <> 0 Then
MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation
End If

End Sub


Public Function GetHostNameFromIP(ByVal sAddress As String) As String

Dim ptrHosent As Long
Dim hAddress As Long
Dim nbytes As Long

If SocketsInitialize() Then

'convert string address to long
hAddress = inet_addr(sAddress)

If hAddress <> SOCKET_ERROR Then

'obtain a pointer to the HOSTENT structure
'that contains the name and address
'corresponding to the given network address.
ptrHosent = gethostbyaddr(hAddress, 4, AF_INET)

If ptrHosent <> 0 Then

'convert address and
'get resolved hostname
CopyMemory ptrHosent, ByVal ptrHosent, 4
nbytes = lstrlen(ByVal ptrHosent)

If nbytes > 0 Then
sAddress = Space$(nbytes)
CopyMemory ByVal sAddress, ByVal ptrHosent, nbytes
GetHostNameFromIP = sAddress
End If

Else: MsgBox "Call to gethostbyaddr failed."
End If 'If ptrHosent

SocketsCleanup

Else: MsgBox "String passed is an invalid IP."
End If 'If hAddress

Else: MsgBox "Sockets failed to initialize."
End If 'If SocketsInitialize

End Function
shines77 2000-09-05
  • 打赏
  • 举报
回复
API 函数 getHostName() 可以搞掂。

另外枚举采用广播的方法,有回应的机器就可记录其IP。
0xFFCD 2000-09-04
  • 打赏
  • 举报
回复
http://lovevb.2699.com源码大权里有个聊天程序,其中有你要的功能。

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧