首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

gethostbyname 是什么意思?解决办法

2012-04-08 
gethostbyname 是什么意思?原贴http://topic.csdn.net/u/20090603/15/a1967885-e1da-44cc-8c1f-705d507ab2

gethostbyname 是什么意思?
原贴
http://topic.csdn.net/u/20090603/15/a1967885-e1da-44cc-8c1f-705d507ab22a.html
63楼.
利用这个就可以吧域名转换成ip,用楼主方法判断是否可以ping通,这样就完美了

Private Sub Command5_Click()
  Dim k As Integer
  Dim ceshi As String
  Dim baidu As String
  
  baidu = "www.baidu.com"
  ceshi = GetIPByName("www.baidu.com")
  Text2.Text = ceshi
End Sub

各种试 

ceshi = GetIPByName("baidu.com")
....
ceshi = GetIPByName(baidu)
...........
  0 0 怎么都不行.
运行到 
hostent_addr = gethostbyname(name)
的时候
hostent_addr 都等于 0


那括号里到底该是啥.?
要有 http:// ? 要有 www. ? 都不用 ? 可以用表达式么 ? 等等 ........

[解决办法]
gethostbyname用于域名解析,比如:xxx.xxx.com
[解决办法]
没有初始化WINSOCK。
Option Explicit

Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(255) As Byte
szSystemstatus(127) As Byte
wMaxSockets As Long
wMaxUDPDG As Long
dwVendorInfo As Long
End Type
Private Type hostent
hName As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVersion As Long, lpWSAD As WSADATA) As Long
Private Declare Function WSACleanup Lib "ws2_32.dll" () As Long

Private Const INADDR_NONE = -1
Private Declare Function gethostname Lib "ws2_32.dll" (ByVal szHost As String, ByVal dwHostLen As Long) As Long
Private Declare Function gethostbyname Lib "ws2_32.dll" (ByVal hostname As String) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As Long

Sub main()
Dim WSAD As WSADATA
Dim lngVersionRequested As Long

'初始化SOCKET函数库
lngVersionRequested = &H202
WSAStartup lngVersionRequested, WSAD

Debug.Print DomainToIp("www.163.com")

WSACleanup
End Sub

Private Function DomainToIp(ByVal strHost As String) As String
Dim lpHostent As Long
Dim lpIPList As Long
Dim lngIP As Long
Dim udtHostent As hostent
Dim bytIP(3) As Byte

lngIP = INADDR_NONE
strHost = Trim(strHost)
If Len(strHost) = 0 Then Exit Function

'判断参数strHost是否为IP地址
lngIP = inet_addr(strHost)
If lngIP = INADDR_NONE Then
'判断参数strHost是否为域名
lpHostent = gethostbyname(strHost & vbNullChar) '根据域名获得IP
If lpHostent = 0 Then Exit Function
CopyMemory udtHostent, ByVal lpHostent, LenB(udtHostent)
CopyMemory lpIPList, ByVal udtHostent.hAddrList, 4
CopyMemory lngIP, ByVal lpIPList, 4 '当域名有多个IP地址时,只取第一个IP
End If

'转换IP地址为字符串
CopyMemory bytIP(0), lngIP, 4
DomainToIp = bytIP(0) & "." & bytIP(1) & "." & bytIP(2) & "." & bytIP(3)
End Function

[解决办法]
lz的hostent_addr=0我想很可能是socket没有初始化造成的,socket使用前必须初始化,程序退出时必须清除。参考以下代码:

窗体的代码:

VB code
Option ExplicitPrivate Sub Command1_Click()Dim sip As Stringsip = getip("www.google.com")End SubPrivate Sub Form_Load()SocketsInitializeEnd SubPrivate Sub Form_Unload(Cancel As Integer)SocketsCleanupEnd SubPrivate Sub SocketsCleanup()  Dim lReturn As Long      lReturn = WSACleanup()      If lReturn <> 0 Then      MsgBox "Socket错误 " & Trim$(Str$(lReturn)) & " occurred in Cleanup "      End  End IfEnd SubPrivate Function hibyte(ByVal wParam As Integer)    '注释:获得整数的高位  hibyte = wParam And &H100 And &HFF&End FunctionPrivate Function lobyte(ByVal wParam As Integer)    '注释:获得整数的低位  lobyte = wParam And &HFF&End FunctionPrivate Function SocketsInitialize()  Dim WSAD As WSADATA  Dim iReturn As Integer  Dim sLowByte As String, sHighByte As String, sMsg As String      iReturn = WSAStartup(WS_VERSION_REQD, WSAD)      If iReturn <> 0 Then      MsgBox "Winsock.dll 没有反应."      End  End If      If lobyte(WSAD.wversion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wversion) = WS_VERSION_MAJOR And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then      sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))      sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))      sMsg = "Windows Sockets版本 " & sLowByte & "." & sHighByte      sMsg = sMsg & " 不被winsock.dll支持 "      MsgBox sMsg      End  End If      If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then      sMsg = "这个系统需要的最少Sockets数为 "      sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD))      MsgBox sMsg      End  End If    End FunctionPrivate Function getip(name As String) As String  Dim hostent_addr As Long  Dim host As HOSTENT  Dim hostip_addr As Long  Dim temp_ip_address() As Byte  Dim i As Integer  Dim ip_address As String      hostent_addr = gethostbyname(name)      If hostent_addr = 0 Then      getip = ""                    '注释:主机名不能被解释      Exit Function  End If      RtlMoveMemory host, hostent_addr, LenB(host)  RtlMoveMemory hostip_addr, host.hAddrList, 4      ReDim temp_ip_address(1 To host.hLength)  RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength      For i = 1 To host.hLength      ip_address = ip_address & temp_ip_address(i) & "."  Next  ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)      getip = ip_addressEnd Function 

热点排行