DOS命令中PING可以解析域名但是好象隐藏了端口号,但是如何将IP地址返回到程序里呢!!!
谢谢

解决方案 »

  1.   

    需要控件 list1 text1 cmdGet(commandButton) cmdExit(commandButton)
    __________________________________________________________________
    modWinsock.Bas (模块)
    Option ExplicitPublic Const INADDR_NONE = &HFFFF
    Public Const SOCKET_ERROR = -1
    Public Const WSABASEERR = 10000
    Public Const WSAEFAULT = (WSABASEERR + 14)
    Public Const WSAEINVAL = (WSABASEERR + 22)
    Public Const WSAEINPROGRESS = (WSABASEERR + 50)
    Public Const WSAENETDOWN = (WSABASEERR + 50)
    Public Const WSASYSNOTREADY = (WSABASEERR + 91)
    Public Const WSAVERNOTSUPPORTED = (WSABASEERR + 92)
    Public Const WSANOTINITIALISED = (WSABASEERR + 93)
    Public Const WSAHOST_NOT_FOUND = 11001
    Public Const WSADESCRIPTION_LEN = 257
    Public Const WSASYS_STATUS_LEN = 129
    Public Const WSATRY_AGAIN = 11002
    Public Const WSANO_RECOVERY = 11003
    Public Const WSANO_DATA = 11004Public Type WSAData
        wVersion       As Integer
        wHighVersion   As Integer
        szDescription  As String * WSADESCRIPTION_LEN
        szSystemStatus As String * WSASYS_STATUS_LEN
        iMaxSockets    As Integer
        iMaxUdpDg      As Integer
        lpVendorInfo   As Long
    End TypePublic Type HOSTENT
        hName     As Long
        hAliases  As Long
        hAddrType As Integer
        hLength   As Integer
        hAddrList As Long
    End TypePublic Type servent
        s_name    As Long
        s_aliases As Long
        s_port    As Integer
        s_proto   As Long
    End TypePublic Type protoent
        p_name    As String     '协议的官方名
        p_aliases As Long       'Null-terminated array of alternate names
        p_proto   As Long       'Protocol number, in host byte order
    End TypePublic Declare Function WSACleanup Lib "ws2_32.dll" () As LongPublic Declare Function inet_ntoa Lib "ws2_32.dll" (ByVal inn As Long) As LongPublic Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As LongPublic Declare Function htonl Lib "ws2_32.dll" (ByVal hostlong As Long) As LongPublic Declare Function ntohl Lib "ws2_32.dll" (ByVal netlong As Long) As LongPublic Declare Function ntohs Lib "ws2_32.dll" (ByVal netshort As Integer) As IntegerPublic Declare Function htons Lib "ws2_32.dll" (ByVal hostshort As Integer) As IntegerPublic Declare Function getprotobynumber Lib "ws2_32.dll" (ByVal proto As Long) As LongPublic Declare Function gethostbyname Lib "ws2_32.dll" (ByVal host_name As String) As LongPublic Declare Function getprotobyname Lib "ws2_32.dll" (ByVal proto_name As String) As LongPublic Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As LongPublic Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVR As Long, lpWSAD As WSAData) As LongPublic Declare Function getservbyport Lib "ws2_32.dll" (ByVal port As Integer, ByVal proto As Long) As LongPublic Declare Function gethostbyaddr Lib "ws2_32.dll" (addr As Long, ByVal addr_len As Long, _
                                                            ByVal addr_type As Long) As LongPublic Declare Function gethostname Lib "ws2_32.dll" (ByVal host_name As String, ByVal namelen As Long) As LongPublic Declare Function getservbyname Lib "ws2_32.dll" (ByVal serv_name As String, ByVal proto As String) As LongPublic Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, _
                                                                     ByVal lpString2 As Long) As LongPrivate Const OFFSET_4 = 4294967296#
    Private Const MAXINT_4 = 2147483647
    Private Const OFFSET_2 = 65536
    Private Const MAXINT_2 = 32767Public Function UnsignedToLong(Value As Double) As Long
        'The function takes a Double containing a value in the range of an unsigned Long
        'and returns a Long that you can pass to an API that requires an unsigned Long
        
        If Value < 0 Or Value >= OFFSET_4 Then Error 6          '溢出
        If Value <= MAXINT_4 Then
            UnsignedToLong = Value
        Else
            UnsignedToLong = Value - OFFSET_4
        End If
    End FunctionPublic Function LongToUnsigned(Value As Long) As Double
    '这个函数从API中取一个无符号长取型数,将它转换成一个双精度型
        If Value < 0 Then
            LongToUnsigned = Value + OFFSET_4
        Else
            LongToUnsigned = Value
        End If
    End FunctionPublic Function UnsignedToInteger(Value As Long) As Integer
        'The function takes a Long containing a value in the range of an unsigned Integer and
        'returns an Integer that you can pass to an API that requires an unsigned Integer
        
        If Value < 0 Or Value >= OFFSET_2 Then Error 6 ' Overflow
        If Value <= MAXINT_2 Then
            UnsignedToInteger = Value
        Else
            UnsignedToInteger = Value - OFFSET_2
        End If
    End FunctionPublic Function IntegerToUnsigned(Value As Integer) As Long
        'The function takes an unsigned Integer from and API and?
        'converts it to a Long for display or arithmetic purposes
        If Value < 0 Then
            IntegerToUnsigned = Value + OFFSET_2
        Else
            IntegerToUnsigned = Value
        End If
    End FunctionPublic Function StringFromPointer(ByVal lPointer As Long) As String
        Dim strTemp As String
        Dim lRetVal As Long    strTemp = String$(lstrlen(ByVal lPointer), 0)           '准备strTemp字符串的缓存
        lRetVal = lstrcpy(ByVal strTemp, ByVal lPointer)        '复制字符串到strTemp缓存中
        If lRetVal Then StringFromPointer = strTemp             '返回一个字符串
    End Function__________________________________________________________________
      

  2.   


    frmMain.frm (主窗体)
    Private Sub cmdGet_Click()
        '通过gethostbyname函数得到HOSTENT结构
    Dim lngPtrToHOSTENT As Long
        Dim udtHostent      As HOSTENT          '存储所有host信息的结构
        Dim lngPtrToIP      As Long             '指向IP地址列表
        Dim arrIpAddress()  As Byte             'byte array that contains elemets of an IP address
        Dim strIpAddress    As String           '将得到IP地址增加到列表框中
        List1.Clear                             '清除List1列表框
                                                '调用Winsock的getthostbyname函数,得到HOSTENT结构
        lngPtrToHOSTENT = gethostbyname(Trim$(Text1.Text))
        '
        If lngPtrToHOSTENT = 0 Then             '检测lngPtrToHOSTENT的值
            '如果gethostbyname函数返回0,则函数执行失败。要得到错误说明,请调用ShowErrorMsg子例程。
            ShowErrorMsg (Err.LastDllError)
        Else
            'gethostbyname函数找到一个IP地址,复制找到的数据到udtHostent结构中
            RtlMoveMemory udtHostent, lngPtrToHOSTENT, LenB(udtHostent)
            'Now udtHostent.hAddrList member contains an array of IP addresses
            RtlMoveMemory lngPtrToIP, udtHostent.hAddrList, 4                   '指向第一个IP地址
            
            Do Until lngPtrToIP = 0
                ReDim arrIpAddress(1 To udtHostent.hLength)                     '准备一个数组来接收IP地址值
                RtlMoveMemory arrIpAddress(1), lngPtrToIP, udtHostent.hLength   '移动IP地址的值到数级中
                For i = 1 To udtHostent.hLength                                 '用IP地址来建立字符串
                    strIpAddress = strIpAddress & arrIpAddress(i) & "."
                Next
                
                strIpAddress = Left$(strIpAddress, Len(strIpAddress) - 1)       '移走最后一个点
                List1.AddItem strIpAddress                                      '增加IP地址到列表框中
                strIpAddress = ""                                               '清除缓存
                udtHostent.hAddrList = udtHostent.hAddrList + LenB(udtHostent.hAddrList)    '指向下一个IP地址
                RtlMoveMemory lngPtrToIP, udtHostent.hAddrList, 4
             Loop
        End If
    End SubPrivate Sub Form_Load()
    Dim lngRetVal      As Long
    Dim strErrorMsg    As String
    Dim udtWinsockData As WSAData
    Dim lngType        As Long
    Dim lngProtocol    As Long
        
        lngRetVal = WSAStartup(&H101, udtWinsockData)       '启动Winsock服务
        If lngRetVal <> 0 Then
            Select Case lngRetVal
                Case WSASYSNOTREADY
                    strErrorMsg = "The underlying network subsystem is not " & _
                        "ready for network communication."
                Case WSAVERNOTSUPPORTED
                    strErrorMsg = "The version of Windows Sockets API support " & _
                        "requested is not provided by this particular " & _
                        "Windows Sockets implementation."
                Case WSAEINVAL
                    strErrorMsg = "The Windows Sockets version specified by the " & _
                        "application is not supported by this DLL."
            End Select
            
            MsgBox strErrorMsg, vbCritical
        End If
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        Call WSACleanup
    End SubPrivate Sub ShowErrorMsg(lngError As Long)
    Dim strMessage As String
        
        Select Case lngError
            Case WSANOTINITIALISED
                strMessage = "A successful WSAStartup call must occur " & "before using this function."
            Case WSAENETDOWN
                strMessage = "The network subsystem has failed."
            Case WSAHOST_NOT_FOUND
                strMessage = "Authoritative answer host not found."
            Case WSATRY_AGAIN
                strMessage = "Nonauthoritative host not found, or server failure."
            Case WSANO_RECOVERY
                strMessage = "A nonrecoverable error occurred."
            Case WSANO_DATA
                strMessage = "Valid name, no data record of requested type."
            Case WSAEINPROGRESS
                strMessage = "A blocking Windows Sockets 1.1 call is in " & _
                             "progress, or the service provider is still " & _
                             "processing a callback function."
            Case WSAEFAULT
                strMessage = "The name parameter is not a valid part of " & "the user address space."
            Case WSAEINTR
                strMessage = "A blocking Windows Socket 1.1 call was " & "canceled through WSACancelBlockingCall."
        End Select
        
        MsgBox strMessage, vbExclamation
    End SubPrivate Sub cmdExit_Click()
        Unload Me
    End Sub___________________________________________________VB6 WindowXP SP2 调试通过
      

  3.   

    [名称]           vb中从域名得到IP及从IP得到域名[语言种类]       Visual Basic[类别一]         因特网[类别二]         网络通信[类别三]         空[数据来源]       未知[来源时间]       未知[保存时间]       2003-01-03[关键字一]       域名[关键字二]       IP[关键字三]       API[文件列表]       空[内容简介]       空[心得体会]       空[源代码内容]Private Const WS_VERSION_REQD = &H101
    Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
    Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
    Private Const MIN_SOCKETS_REQD = 1
    Private Const SOCKET_ERROR = -1
    Private Const WSADescription_Len = 256
    Private Const WSASYS_Status_Len = 128Private Type HOSTENT
       hname As Long
       hAliases As Long
       hAddrType As Integer
       hLength As Integer
       hAddrList As Long
    End TypePrivate 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
       iMaxUdpDg As Integer
       lpszVendorInfo As Long
    End Type
    Private Declare Function gethostbyaddr Lib "WSOCK32.DLL" (addr As Any, ByVal _
    byteslen As Integer, addrtype As Integer) As Long
    Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
    Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal _
            wVersionRequired&, lpWSAData As WSADATA) As Long
    Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
    Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal _
            hostname$) As Long
    Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, _
            ByVal hpvSource&, ByVal cbCopy&)Function hibyte(ByVal wParam As Integer)    '获得整数的高位
       hibyte = wParam \ &H100 And &HFF&
    End FunctionFunction lobyte(ByVal wParam As Integer)    '获得整数的低位
       lobyte = wParam And &HFF&
    End FunctionFunction 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 FunctionSub SocketsCleanup()
       Dim lReturn As Long
       
       lReturn = WSACleanup()
       
       If lReturn <> 0 Then
          MsgBox "Socket错误 " & Trim$(str$(lReturn)) & " occurred in Cleanup "
          End
       End If
    End Sub
    Sub Form_Load()
        '初始化Socket
        SocketsInitialize
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        '清除Socket
        SocketsCleanup
    End Sub
    Private 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 FunctionPrivate Sub Command1_click()
        Dim str As String
        str = getip(Text1.Text)
        If str = "" Then
            Text2.Text = "主机名不能被解释"
        Else
            Text2.Text = str
        End If
    End Sub
    Private Function getname(addrstr As String) As String
        Dim hostent_addr As Long
        Dim host As HOSTENT
        Dim addr(0 To 50) As Byte
        Dim addrs As String
        Dim hname(1 To 50) As Byte
        Dim str As String
        Dim i As Integer, j As Integer
        Dim temp_int As Integer
        Dim byt As Byte
        str = Trim$(addrstr)
        i = 0
        j = 0
        Do
            temp_int = 0
            i = i + 1
            Do While Mid$(str, i, 1) >= "0" And Mid$(str, i, 1) <= "9" And i <= Len(str)
                temp_int = temp_int * 10 + Mid$(str, i, 1)
                i = i + 1
            Loop
            If temp_int <= 255 Then
                addr(j) = temp_int
                j = j + 1
            End If
        
        Loop Until Mid$(str, i, 1) <> "." Or i > Len(str) Or temp_int > 255
        If temp_int > 255 Then
            getname = "地址非法"
            Exit Function
        End If
        hostent_addr = gethostbyaddr(addr(0), j, 2)
        If hostent_addr = 0 Then
            getname = "此地址无法解析"
            Exit Function
        End If
        RtlMoveMemory host, hostent_addr, LenB(host)
        RtlMoveMemory hname(1), host.hname, 50
        j = 51
        For i = 1 To 50
            If hname(i) = 0 Then
                j = i
            End If
            If i >= j Then
                hname(i) = 32
            End If
        Next i
        getname = Trim$(StrConv(hname, vbUnicode))
    End Function
    Private Sub Command2_Click()
        Dim name As String
        name = getname(Text2.Text)
        If name = "" Then
            name = "此地址没有域名"
        End If
        Text1.Text = name
    End Sub
         以上代码保存于: SourceCode Explorer(源代码数据库)
               复制时间: 2005-10-24 21:54:55
               软件版本: 1.0.881
               软件作者: Shawls
                 E-Mail: [email protected]
                     QQ: 9181729
      

  4.   

    楼上 shawls 的代码不错。收藏,thanks!
      

  5.   

    好费劲啊,俺来两句:
     Shell "cmd /c ping www.163.com >d:\ip.txt", vbHide '生成ip.txt
    然后读取ip.txt,不就OK了么?