none
判斷是否可連上某網址的程式碼在xp和vista執行結果怎麼會不一樣? RRS feed

  • 問題

  • 下面是我寫的一段判斷是否能連上某網址或ip的程式碼,我在vista上執行,
    連上tw.yahoo.com,www.google.com,168.95.1.1這些網址或ip都能return
    true,但在xp上執行卻全部timeout,連不上,vb.net是跨平台,因該不會
    有不同結果才對,想請大大們請教一下是哪個地方有問題?
    非常感謝!

    Public Shared Function canPing(ByVal ipOrHostname As String, ByVal isIp As Boolean) As Boolean
    
                ipOrHostname = ipOrHostname.Trim()
                Dim host As System.Net.IPHostEntry
                Dim ip As String
                If isIp = False Then
                    Try
                        host = System.Net.Dns.GetHostEntry(ipOrHostname)
                        ip = host.AddressList.GetValue(0).ToString()
    
                    Catch ex As Exception
                        Return False
                    End Try
                Else
                    ip = ipOrHostname
                End If
    
    
                Dim p As System.Net.NetworkInformation.Ping
                p = New System.Net.NetworkInformation.Ping()
                Dim options As PingOptions
                options = New PingOptions()
                options.DontFragment = True
                Dim data As String
                data = "Test Data!"
                Dim buffer() As Byte
                buffer = Encoding.ASCII.GetBytes(data)
                Dim timeout As Integer
                timeout = 1000
                Dim reply As PingReply
                reply = p.Send(ip, timeout, buffer, options)
                If reply.Status = IPStatus.Success Then
                    Return True
                Else
                    Return False
    
                End If
    
            End Function
    2009年10月23日 上午 02:28

解答

所有回覆