前段日子做了一个和扫描枪通讯的程序,连接扫描枪时需要先ping 一下ip地址,ping得通就去连,不通就不去连。
1 /// <summary> 2 /// ping ip,测试能否ping通 3 /// </summary> 4 /// <param name="strIP">IP地址</param> 5 /// <returns></returns> 6 private bool PingIp(string strIP) 7 { 8 bool bRet = false; 9 try 10 { 11 Ping pingSend = new Ping(); 12 PingReply reply = pingSend.Send(strIP, 1000); 13 if (reply.Status == IPStatus.Success) 14 bRet = true; 15 } 16 catch (Exception) 17 { 18 bRet = false; 19 } 20 return bRet; 21 } 22 23 24