Java模擬Ping主機和Telnet服務的知識點和程式碼整理總結

我就是曹總發表於2019-05-11

伺服器由於提供方,說沒問題,但是我們測試很不穩定,就使用java模擬ping了下服務,超過5秒自動超時,程式碼如下所示

package com.isoftstone.clusterdeployment.util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class IoPing {

	/**
	 * 進行ping主機
	 */
	public static boolean ioPing(String ip) {
		boolean isping = false;
		try {
			InetAddress address = InetAddress.getByName(ip);
			isping = address.isReachable(5000);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return isping;

	}

	/**
	 * telnet程式
	 * 
	 * @param ipListStr
	 * @param port
	 */
	public static boolean ioTelnet(String ipListStr, String port) {
		boolean telnetstr = false;
		Socket server = null;
		try {
			server = new Socket();
			InetSocketAddress address = new InetSocketAddress(ipListStr,
					Integer.parseInt(port));
			server.connect(address, 5000);
			telnetstr = true;
		} catch (UnknownHostException e) {
			System.out.println("telnet失敗");
			telnetstr = false;
		} catch (IOException e) {
			System.out.println("telnet失敗");
			telnetstr = false;
		} finally {
			if (server != null)
				try {
					server.close();
				} catch (IOException e) {
				}
		}

		return telnetstr;
	}
	
	
	
	public static void main(String[] args) {
		System.err.println(IoPing.ioTelnet("10.16.124.148", "22"));
	}
	
	

}



這樣,需要狀態,隨時提供,現在扯皮滴事情,就需要證據.才有說服力

請喊我大龍哥最後編輯於:3年前

內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。

相關文章