public static Data StringLine2Data(string line) { var s = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); if (s.Length < 1) { return(null); } string hostname; IPAddress ip = null; var port = 443; var sp = s[0].Split(':'); if (sp.Length == 1 || sp.Length == 2) { hostname = sp[0]; if (IPFormatter.IsIPv4Address(hostname)) { ip = IPAddress.Parse(hostname); } } else { return(null); } if (sp.Length == 2) { try { port = Convert.ToInt32(sp[1]); } catch { return(null); } if (!IPFormatter.IsPort(port)) { return(null); } } var res = new Data { HostsName = hostname, Ip = ip, Port = port, Description = string.Empty }; if (s.Length == 2) { res.Description = s[1]; } return(res); }
private void FirstPing() { var t = new Task(() => { Parallel.For(0, mainTable.Count, (i, state) => { try { cts_PingTask.Token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { state.Stop(); return; } if (IPFormatter.IsIPv4Address(mainTable[i].HostsName)) //反查DNS { PingOne(i); try { cts_PingTask.Token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { state.Stop(); return; } mainTable[i].HostsName = NetTest.GetHostName(IPAddress.Parse(mainTable[i].HostsName), ReverseDNSTimeout); } else { var ip = NetTest.GetIP(mainTable[i].HostsName); try { cts_PingTask.Token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { state.Stop(); return; } if (ip != null) { mainTable[i].Endpoint = $@"{ip}:{rawTable[i].Port}"; } PingOne(i); } try { cts_PingTask.Token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { state.Stop(); } }); //notifyIcon1.ShowBalloonTip(1000, ExeName, @"载入完毕", ToolTipIcon.Info); }); PingTasks.Add(t); t.Start(); }