遇到字符串,数组和IP地址问题(VB.NET)

时间:2016-05-22 02:07:14

标签: arrays vb.net string list ip

我有一段代码可以ping NIST服务器列表并检索它们的时间。这是完美的原始代码:

'Server IP addresses from 
'http://tf.nist.gov/tf-cgi/servers.cgi - current as of 05/20/2016
Private Shared Servers() As String = {
      "129.6.15.28" _
    , "129.6.15.29" _
    , "129.6.15.30" _
    , "98.175.203.200" _
    , "66.199.22.67" _
    , "64.113.32.5" _
    , "198.111.152.100" _
    , "216.229.0.179" _
    , "24.56.178.140" _
    , "132.163.4.101" _
    , "132.163.4.102" _
    , "132.163.4.103" _
    , "128.138.140.44" _
    , "128.138.141.172" _
    , "198.60.73.8" _
    , "131.107.13.100" _
    , "216.228.192.69"
}

Public Shared LastHost As String = ""
Public Shared LastSysTime As DateTime

Public Shared Function GetTime() As DateTime
    'Returns UTC/GMT using an NIST server if possible, 
    ' degrading to simply returning the system clock

    'If we are successful in getting NIST time, then
    ' LastHost indicates which server was used and
    ' LastSysTime contains the system time of the call
    ' If LastSysTime is not within 15 seconds of NIST time,
    '  the system clock may need to be reset
    ' If LastHost is "", time is equal to system clock

    Dim host As String
    Dim result As DateTime

    LastHost = ""
    For Each host In Servers
        Try
            If My.Computer.Network.Ping(host) Then
                LastHost = host
                result = GetNISTTime(host)
            End If
        Catch ex As Exception
            MsgBox("There was an error retrieving the internet time. Please try again.", MsgBoxStyle.Exclamation, "Sync Error")
        End Try
    Next

    If LastHost = "" Then
        'No server in list was successful so use system time
        result = DateTime.UtcNow()
    End If

    Return result
End Function

但是,我正在尝试修改它,以便我可以使用保存在My.Settings或My.Resources文件中的NIST服务器IP地址列表(因此我可以轻松修改I地址)。所以基本上就像上面的工作代码一样沿着每一行走下去。除了以下代码不能像上面的代码一样工作;它只是返回“System.Strings []”的行:

'Server IP addresses from 
'http://tf.nist.gov/tf-cgi/servers.cgi - current as of 05/20/2016
'Declares stting that loads server IP addresses
Public Shared VanServ As String = My.Resources.VanillaServerIPList
Public Shared Serv As String = My.Settings.ServerIPList
Public Shared Servers() As String


Public Shared LastHost As String = ""
Public Shared LastSysTime As DateTime

Public Shared Function GetTime() As DateTime
    'Returns UTC/GMT using an NIST server if possible, 
    ' degrading to simply returning the system clock

    'If we are successful in getting NIST time, then
    ' LastHost indicates which server was used and
    ' LastSysTime contains the system time of the call
    ' If LastSysTime is not within 15 seconds of NIST time,
    '  the system clock may need to be reset
    ' If LastHost is "", time is equal to system clock
    Servers = Serv.Split(vbCrLf)

    Dim host As String
    Dim result As DateTime

    LastHost = ""
    For Each host In Servers
        Try
            If My.Computer.Network.Ping(host) Then
                LastHost = host
                result = GetNISTTime(host)
            End If
        Catch ex As Exception
            MsgBox("There was an error retrieving the internet time. Please try again.", MsgBoxStyle.Exclamation, "Sync Error")
        End Try
    Next

    If LastHost = "" Then
        'No server in list was successful so use system time
        result = DateTime.UtcNow()
    End If

    Return result
End Function

My.Resources.VanillaServerIPList和My.Settings.ServerIPList如下所示(在您说检查列表是否为空之前,两个列表实际上都包含以下值):

129.6.15.28

129.6.15.29

129.6.15.30

98.175.203.200

66.199.22.67

64.113.32.5

198.111.152.100

216.229.0.179

......等等;每个IP都在新的一行

1 个答案:

答案 0 :(得分:0)

以下是我最终登陆的内容:

end