首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

java施用ping命令

2012-07-22 
java使用ping命令一.先来熟悉一下ping命令吧:ping:(常用)参数:解释(默认值)实例:-t ? ? ? ? ? ? 直到使用c

java使用ping命令

一.先来熟悉一下ping命令吧:

ping:(常用)

参数:

解释(默认值)实例:

-t ? ? ? ? ? ?

直到使用ctrl+c停止执行完自动停止无


-a ? ? ? ? ? ?

将地址解析成主机名无:ping?-a?192.168.1.114

?

?-n count ?

?每ping一次得到的响应回复的消息条数(默认值:4):ping -n 10?192.168.1.114?

?

-l size ? ? ? ?

每次请求的包大小:count(0到65500Byte之间) (默认值:32byte):ping?-l 65500?192.168.1.114

?


?

?

二.图形化界面:操作ping(线程/进程)

?

package com.qingshan.utils.ping;import java.awt.Color;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import java.util.regex.Pattern;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;public class PingGUI implements Runnable {private JFrame frame;//框架private JButton start;//开启ping线程/进程private JButton end;//不再开启ping线程/进程private JButton endHave;//结束已经开启的ping线程/进程private JButton finalEnd;//线束所有ping.exeprivate static String ip;//目标ip/域名private static long threadCount;////线程数量private static int processCount;//进程数量private List<Thread> thread;//储存所有已开启的线程private JTextField processCountText;//进程数量输入框private JTextField threadCountText;//线程数量输入框private JLabel topLabel;//提示错误信息private JTextArea statusLable;//ping线程/进程执行状况private JTextField ipText;//目标ip/域名输入框private boolean goon = true;//是否继续开启ping线程/进程private static Ping p = new Ping();//使用系统皮肤static {try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (UnsupportedLookAndFeelException e) {e.printStackTrace();}}@Overridepublic void run() {try {System.err.println("启动...");for (long i = 0; i < processCount; i++) {if(!goon){return ;}p.runPing("ping " + ip + " -l 65500 -f");}System.err.println("运行结束...");} catch (IOException e) {topLabel.setText("线程:" + e);Thread.currentThread().interrupt();Thread.currentThread().stop();}}private void initGUI() {frame = new JFrame("ping 工具");frame.setBounds(400, 100, 600, 300);frame.setVisible(true);frame.setAlwaysOnTop(true);frame.setLayout(new GridLayout(6, 1));frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});start = new JButton("开始ping");start.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {initData();}});end = new JButton("不再开启ping线程/进程");end.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {noOpne();}});endHave = new JButton("关闭已经开启的ping线程");endHave.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {closeAllThread();}});finalEnd = new JButton("结束所有ping.exe");finalEnd.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {stopPing();}});JPanel top = new JPanel(new FlowLayout(FlowLayout.LEADING));top.add(new JLabel("ping目标地址:"));ipText = new JTextField("www.google.com.hk", 76);top.add(ipText);JPanel threadCountPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));threadCountPanel.add(new JLabel("开启线程总数:"));threadCountText = new JTextField("100", 76);threadCountPanel.add(threadCountText);JPanel processCountPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));processCountPanel.add(new JLabel("开启进程总数:"));processCountText = new JTextField("1000000", 76);processCountText.setToolTipText("每条线程所有开启的进程数");processCountPanel.add(processCountText);JPanel buttom = new JPanel(new FlowLayout(FlowLayout.LEADING));buttom.add(start);buttom.add(end);buttom.add(endHave);buttom.add(finalEnd);topLabel = new JLabel("提示信息....");frame.add(topLabel);frame.add(top);frame.add(threadCountPanel);frame.add(processCountPanel);frame.add(buttom);statusLable = new JTextArea("状态展示....", 20, 420);statusLable.setEditable(false);frame.add(statusLable);}private void initData() {thread = new ArrayList<Thread>();ip = ipText.getText();try {threadCount = Long.parseLong(threadCountText.getText());System.out.println(threadCount);} catch (Exception e) {topLabel.setText("请在开启线程总数输入框输入---数字");topLabel.setForeground(Color.red);return ;}try {processCount = Integer.parseInt(processCountText.getText());System.out.println(processCount);} catch (Exception e) {topLabel.setText("请在开启进程总数输入框输入---数字");topLabel.setForeground(Color.red);return ;}//将界面与逻辑分开Thread t = new Thread(){@Overridepublic void run() {startAllThread();}};t.start();goon = true;}private void startAllThread() {for (int i = 0; i < threadCount; i++) {if(!goon){return ;}Thread t = new Thread(new PingGUI());t.start();System.out.println(t.getName()+"\t线程开启");thread.add(t);}}private void closeAllThread() {if(thread.size() < 1){System.out.println("线程已全部闭关或未开启任何线程");return ;}for (Thread t : thread) {System.out.println(t.getName()+"线程关闭");t.interrupt();t.stop();}thread.clear();}private void noOpne(){goon = false;}private void stopPing() {try {noOpne();closeAllThread();p.runPing("taskkill /f /im ping.exe");} catch (IOException e) {System.err.println(e);}}public static void main(String[] args) throws IOException {PingGUI p = new PingGUI();p.initGUI();}}

?

?

三.所写的代码的核心所在:

?

package com.qingshan.utils.ping;import java.io.IOException;import java.io.InputStream;public class Ping {public String runPing(String cmd) throws IOException {System.out.println(cmd);// 建立并执行进程Process p = Runtime.getRuntime().exec(cmd);// 获取输入流并打印输出信息InputStream is = p.getInputStream();byte[] b = new byte[1024];StringBuffer sb = new StringBuffer();while (is.read(b) != -1) {sb.append(new String(b));}// 关闭输入流if (is != null) {is.close();}p.destroy();System.err.println(sb.toString()+"\n------------------------------\n");return sb.toString();}}
?

注:2012-5-25更新--->对ping进行了补充

?

转载注明原文地址:http://thetopofqingshan.iteye.com/admin/blogs/1539237

热点排行