日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pinger2

發布時間:2025/5/22 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pinger2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;


public class Pinger{
?? ?// IP地址
?? ?private String ip;
?? ?
?? ?// Ping次數?? ?
?? ?private int timesNum;
?? ?
?? ?// 開始時間
?? ?private String startTime;

?? ?// 結束時間
?? ?private String endTime;
?? ?
?? ?/**
?? ? * 構造函數
?? ? * @param ip
?? ? * @param timesNum
?? ? */
?? ?public Pinger(String ip,int timesNum){
?? ??? ?this.ip=ip;
?? ??? ?this.timesNum=timesNum;
?? ?}
?? ?
?? ?/**
?? ? * Ping后計算響應的個數
?? ? * @return
?? ? * @throws Exception
?? ? */
?? ?public int countReply() throws Exception{
?? ??? ?String destIp=ip;
?? ??? ?int maxCount=timesNum;
?? ??? ?
?? ??? ?LineNumberReader input = null;
??????? try {
?????? ??? ?startTime=DateTimeUtil.getCurrDateTime();
?????? ??? ?
?????? ??? ?// 根據操作系統組合命令
??????????? String osName = System.getProperties().getProperty("os.name");
??????????? String pingCmd = null;
??????????? if (osName.startsWith("Windows")) {
?????????????? pingCmd = "cmd /c ping -n {0} {1}";
??????????????? pingCmd = MessageFormat.format(pingCmd, maxCount, destIp);
??????????? } else if (osName.startsWith("Linux")) {
??????????????? pingCmd = "ping -c {0} {1}";
??????????????? pingCmd = MessageFormat.format(pingCmd, maxCount, destIp);
??????????? } else {
??????????????? throw new Exception("not support OS");
??????????? }
?????????? ?
??????????? // ping完得到響應
??????????? Process process = Runtime.getRuntime().exec(pingCmd);
??????????? InputStreamReader ir = new InputStreamReader(process
??????????????????? .getInputStream());
??????????? input = new LineNumberReader(ir);
??????????? String line;
??????????? List<String> reponse = new ArrayList<String>();

??????????? while ((line = input.readLine()) != null) {
??????????????? if (!"".equals(line)) {
??????????????????? reponse.add(line);
??????????????????? // System.out.println("====:" + line);
??????????????? }
??????????? }
?????????? ?
??????????? // 分析響應
??????????? if (osName.startsWith("Windows")) {
??????????????? return parseWindowsMsg(reponse, maxCount);
??????????? } else if (osName.startsWith("Linux")) {
??????????????? return parseLinuxMsg(reponse, maxCount);
??????????? }

??????? } catch (IOException e) {
??????????? System.out.println("IOException?? " + e.getMessage());

??????? } finally {
??????????? if (null != input) {
??????????????? try {
??????????????????? input.close();
??????????????? } catch (IOException ex) {
??????????????????? System.out.println("close error:" + ex.getMessage());

??????????????? }
??????????? }
?????????? ?
??????????? endTime=DateTimeUtil.getCurrDateTime();
??????? }
?? ??? ?
?? ??? ?return 0;
?? ?}
?? ?
?? ?/**
?? ? * ping次數和響應數相等算ping通
?? ? * @return
?? ? * @throws Exception
?? ? */
?? ?public boolean isPass() throws Exception{
?? ??? ?return countReply()==timesNum;
?? ?}
?? ?
?? ?private int parseWindowsMsg(List<String> reponse, int total) {
??????? int countTrue = 0;
??????? int countFalse = 0;
??????? for (String str : reponse) {
??????????? if (str.startsWith("來自") || str.startsWith("Reply from")) {
??????????????? countTrue++;
??????????? }
??????????? if (str.startsWith("請求超時") || str.startsWith("Request timed out")) {
??????????????? countFalse++;
??????????? }
??????? }
??????? return countTrue;
??? }

??? private int parseLinuxMsg(List<String> reponse, int total) {
??????? int countTrue = 0;
??????? for (String str : reponse) {
??????????? if (str.contains("bytes from") && str.contains("icmp_seq=")) {
??????????????? countTrue++;
??????????? }
??????? }
??????? return countTrue;
??? }
?? ?
?? ?public String getStartTime() {
?? ??? ?return startTime;
?? ?}

?? ?public String getEndTime() {
?? ??? ?return endTime;
?? ?}
?? ?
??? public static void main(String[] args) throws Exception{
?? ??? ?Pinger p=new Pinger("www.163.com",5);
?? ??? ?
?? ??? ?System.out.println(p.countReply());
?? ??? ?System.out.println(p.getStartTime());
?? ??? ?System.out.println(p.getEndTime());
??? }
}

轉載于:https://www.cnblogs.com/xiandedanteng/p/3371730.html

總結

以上是生活随笔為你收集整理的Pinger2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。