public void Run() { hostPlayer = null; List <PlayerPingPair> hostables = new List <PlayerPingPair>(); System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); foreach (MNPlayer player in players) { if (player.canHost) { var pair = new PlayerPingPair(player); var reply = ping.Send(MNListServer.Instance.GetProperAddress(player)); if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { pair.ping = reply.RoundtripTime; } hostables.Add(pair); } } ping.Dispose(); if (hostables.Count <= 0) { return; } PlayerPingPair best = hostables[0]; foreach (PlayerPingPair pair in hostables) { if (best.ping > pair.ping) { best = pair; } } hostPlayer = best.player; }
private void PingUpdateSystem(string in_region, string in_target) { System.Net.NetworkInformation.Ping pinger = new System.Net.NetworkInformation.Ping(); try { pinger.PingCompleted += (o, e) => { if (e.Error == null && e.Reply.Status == System.Net.NetworkInformation.IPStatus.Success) { handlePingTimeResponse(e.Reply.RoundtripTime, in_region); } }; pinger.SendAsync(in_target, null); } catch (System.Net.NetworkInformation.PingException) { // Discard PingExceptions and return false; } finally { if (pinger != null) { pinger.Dispose(); } } }
static bool Internet_Check() { //http://dobon.net/vb/dotnet/internet/ping.html System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); //Pingを送信する try { System.Net.NetworkInformation.PingReply reply = p.Send("api.twitter.com", 3000); //解放 p.Dispose(); //結果を取得しreturn if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { return(true); } else { return(false); } } //もしpingを送信できない場合はfalseを返す catch (System.Net.NetworkInformation.PingException) { return(false); } }
public async void RunPingSweep_Async() { nFound = 0; var tasks = new List <Task>(); stopWatch.Start(); for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { ip = BaseIP + i.ToString() + "." + j.ToString(); System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); var task = PingAndUpdateAsync(p, ip); tasks.Add(task); if (j == 255) { wait.SpinOnce(); } p.Dispose(); } } await Task.WhenAll(tasks).ContinueWith(t => { MainWindow.check_dns_names_kd(); stopWatch.Stop(); }); }
public static FS_NETWORK_STATUS Ping(string host) { FS_NETWORK_STATUS oStatus = FS_NETWORK_STATUS.NotAvailable; string strError = ""; if (host != null && host.Trim().Length > 0) { try { System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); if (p.Send(host, 500).Status == System.Net.NetworkInformation.IPStatus.Success) { oStatus = FS_NETWORK_STATUS.Available; } p.Dispose(); p = null; } catch (Exception e) { strError = e.Message; } } return(oStatus); }
/// <summary> /// Ping応答があるかを返します。 /// </summary> /// <param name="host">送信先のホスト</param> /// <param name="toutms">タイムアウト時間(ms)</param> /// <returns>結果(リプライあり=true, リプライなし=false)</returns> public bool IsPingReply(String host, int toutms) { bool rt = true; System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); try { LogPrint("PING " + host); System.Net.NetworkInformation.PingReply reply = p.Send(host, toutms); if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { LogPrint("PING 応答 - From[" + reply.Address.ToString() + "], Size[" + reply.Buffer.Length.ToString() + "], Time[" + reply.RoundtripTime.ToString() + "ms], TTL[" + reply.Options.Ttl.ToString() + "]"); } else { LogPrint("PING 失敗 - " + reply.Status.ToString()); rt = false; } } catch { rt = false; } p.Dispose(); return(rt); }
public override Task <PingReplyWrapper> SendPingAsync(string hostnameOrAddress, TimeSpan timeout) { ensureNotDisposed(); var _ping = new System.Net.NetworkInformation.Ping(); return(_ping.SendPingAsync(hostnameOrAddress, (int)timeout.TotalMilliseconds).ContinueWith <PingReplyWrapper>(t => { _ping.Dispose(); return new PingReplyWrapper { RoundtripTime = t.Result.RoundtripTime, Status = t.Result.Status }; })); }
private async Task PingAndUpdateAsync(System.Net.NetworkInformation.Ping ping, string ip) { var reply = await ping.SendPingAsync(ip, timeout); if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { if (MainWindow.GetMachineNameFromIPAddress(reply.Address.ToString()).Length > 1) { UsersModel x = new UsersModel(); x.pc_name = MainWindow.GetMachineNameFromIPAddress(reply.Address.ToString()); x.ip = reply.Address.ToString(); UM.Add(x); Console.WriteLine(x.ip + " " + x.pc_name); try { SqliteDataAccess.AddUser(x.pc_name, x.ip); } catch (System.Data.SQLite.SQLiteException) { SqliteDataAccess.UpdateUserIp(x.pc_name, x.ip); } } lock (lockObj) { nFound++; ping.Dispose(); } ping.Dispose(); } if (reply.Status != System.Net.NetworkInformation.IPStatus.Success) { ping.Dispose(); } }
/// <summary> /// 是否能 Ping 通指定的主机 /// </summary> /// <param name="ip">ip 地址或主机名或域名</param> /// <returns>true 通,false 不通</returns> public bool Ping(string ip) { try { System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); int timeout = 500; // Timeout 时间,单位:毫秒 System.Net.NetworkInformation.PingReply reply = ping.Send(ip, timeout); ping.Dispose(); if (reply == null || reply.Status == System.Net.NetworkInformation.IPStatus.Success) { return(true); } return(false); } catch (System.Net.NetworkInformation.PingException) { return(false); } }
void CheckTerminalIsLink(object ips, int interval, int timeout) { if (ips != null) { System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); string ipAddrs = ips.ToString(); string[] ipss = ipAddrs.Split('|'); foreach (string ipAddr in ipss) { System.Net.NetworkInformation.PingReply pr = ping.Send(ipAddr, timeout > interval ? interval : timeout); bool off = pr.Status != System.Net.NetworkInformation.IPStatus.Success; if (off) { ShowStatus("与服务器的连接已断开!"); } else { ShowStatus("Ready..."); } } ping.Dispose(); } }
//パラメータのアドレスに対して、PINGを実行する。 public void ExecPing(Class_keepResult res_cls) { res_cls.newDt = DateTime.Now; //Pingオブジェクトの作成 System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); //IPaddressにPingを送信する 5秒固定 //デフォルトのバッファ・サイズは32bytes System.Net.NetworkInformation.PingReply reply = p.Send(res_cls.address, 5000); //失敗時リトライ //if(reply.Status != System.Net.NetworkInformation.IPStatus.Success) // reply = p.Send(res_cls.address, 5000); //結果を取得 //成功時 if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { //Console.WriteLine("Reply from {0}:bytes={1} time={2}ms TTL={3}", // reply.Address, reply.Buffer.Length, // reply.RoundtripTime, reply.Options.Ttl); //前回成功時 if (res_cls.status == 1) { //復旧カウントをプラス if (res_cls.recoverCount > 0) { res_cls.recoverCount++; } res_cls.changeflg = 0; } // 前回障害 (復旧) else if (res_cls.status == -1) { // 復旧時 logger.Warn(res_cls.address + ": Ping応答復旧。" + reply.Status); res_cls.status = 1; //カウントを0に戻す res_cls.count = 0; //メッセージを消す res_cls.message = ""; //復旧カウントを開始 res_cls.recoverCount = 1; res_cls.changeflg = 1; } //初回 else { res_cls.status = 1; res_cls.changeflg = 0; } } else { // 失敗時 logger.Warn(res_cls.address + ": Ping応答なし。" + reply.Status); res_cls.message = reply.Status.ToString(); // 前回成功時 if (res_cls.status == 1) { res_cls.lastDt = DateTime.Now; //カウントを開始 res_cls.count = 1; res_cls.status = -1; //復旧カウントを0 res_cls.recoverCount = 0; res_cls.changeflg = 1; } // 障害 else if (res_cls.status == -1) { //カウント追加(カウントが振り切れたら1に戻す) res_cls.count = (res_cls.count == long.MaxValue) ? 1 : res_cls.count + 1; res_cls.changeflg = 0; } // 初回 else { res_cls.lastDt = DateTime.Now; res_cls.status = -1; //カウント追加 カウントが振り切れたら1に戻す res_cls.count = (res_cls.count == long.MaxValue) ? 1 : res_cls.count + 1; res_cls.changeflg = 0; } } p.Dispose(); }
static void Main(string[] args) { string ipAddress = "8.8.8.7"; byte[] physicalAddress = { 0, 0, 0, 0, 0, 0 }; //ファイルから、ipアドレスとMACアドレス取得 //自身のパス取得 System.Reflection.Assembly assem = typeof(Program).Assembly; string assemPath = assem.Location; string assemDictionary = Path.GetDirectoryName(assemPath); //設定ファイルフルパス作成 string settingFileName = "Setting.txt"; string settingFilePath = Path.Combine(assemDictionary, settingFileName); //存在確認 if (!File.Exists(settingFilePath)) { setColorError(); Console.WriteLine("設定ファイルが見つかりません。"); Console.WriteLine(settingFilePath); System.Threading.Thread.Sleep(2000); Console.ResetColor(); return; } //ファイルから読み込み var sr = new StreamReader(settingFilePath); try { ipAddress = sr.ReadLine(); string pAddr = sr.ReadLine(); string[] pAddrArr = pAddr.Split('-'); for (int i = 0; i < 6; i++) { physicalAddress[i] = byte.Parse(pAddrArr[i], System.Globalization.NumberStyles.HexNumber); } } catch (Exception ex) { setColorError(); Console.WriteLine("ファイル読み込みに失敗しました。"); Console.WriteLine(ex.Message); System.Threading.Thread.Sleep(2000); return; } finally { sr.Dispose(); Console.ResetColor(); } //pingを送信 Console.WriteLine("pingの送信・・・"); var p = new System.Net.NetworkInformation.Ping(); try { System.Net.NetworkInformation.PingReply reply; try { reply = p.Send(ipAddress); } catch (Exception ex) { setColorError(); Console.WriteLine("例外発生"); Console.WriteLine(ex.Message); if (ex.InnerException != null) { Console.WriteLine(ex.InnerException.Message); } Console.WriteLine("ホスト名 OR IPアドレス:" + ipAddress); Console.Read(); return; } if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { setColorSuccess(); Console.WriteLine("ping正常終了"); Console.WriteLine("サーバーは起動しています。"); System.Threading.Thread.Sleep(2000); return; } else { setColorError(); Console.WriteLine("ping失敗"); } } finally { p.Dispose(); Console.ResetColor(); } Console.WriteLine("マジックパケットを送信します。"); SendMagicPacket(physicalAddress); Console.WriteLine("送信しました。"); System.Threading.Thread.Sleep(2000); }
/// <summary> /// checks client with given port parameters (example: 50000 ports of all servers) /// </summary> private List <ServerInfoSimple> findServer(int port_, bool fasterSearch) { string ip_lan = "192.168.1."; object lockObjectTmp = new object(); int numIps = 0; List <ServerInfoSimple> serverList = new List <ServerInfoSimple>(); int upperLimit = 255; if (fasterSearch) { upperLimit = 10; } Parallel.For(1, upperLimit, i => { string ip = ip_lan + i; Console.WriteLine(ip + ":"); System.Net.NetworkInformation.Ping p = null; try { p = new System.Net.NetworkInformation.Ping(); System.Net.NetworkInformation.PingReply rep = p.Send(ip); if (rep.Status == System.Net.NetworkInformation.IPStatus.Success) { var server = new ServerInfoSimple() { port = port_, roundTripPerformance = 1.0 / (0.1 + (double)rep.RoundtripTime), ipString = (new StringBuilder(ip)).ToString() }; Console.WriteLine(rep.RoundtripTime + "ms"); try { // not works always //IPHostEntry ipHostEntry = Dns.GetHostEntry(ip); //Console.WriteLine(ipHostEntry.HostName); //server.nameOfServer = ipHostEntry.HostName; } catch (Exception ee) { // server olmayabilir server.nameOfServer = "unknown server name"; } if (serverTesting(ip, port_)) { // todo // need to test connection speed before tree-cluster compute // test closes N nodes lock (lockObjectTmp) { numIps++; serverList.Add(server); } } } } catch (Exception e) { Console.WriteLine("ping error"); Console.WriteLine(e.StackTrace); } finally { p.Dispose(); Console.WriteLine("______________________"); } }); Console.WriteLine(numIps + " cluster PCs connected."); foreach (var item in serverList) { Console.WriteLine("---------------"); Console.WriteLine(item.ipString); Console.WriteLine(item.roundTripPerformance); Console.WriteLine(item.nameOfServer); Console.WriteLine("---------------"); } return(serverList); }