package com.leanwo.activiti.util;

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

/**
 * 网络工具类
 * @author YangZhiJie
 *
 */
public class NetUtil {
	
	/***
	 * 判断本地端口是否被占用
	 * @param port 端口号
	 * @return true: 端口被占用，false: 端口未被占用
	 *  
	 */
	public static boolean isLocalPortUsing(int port) {
		boolean flag = true;
		try {
			flag = isPortUsing("127.0.0.1", port);
		} catch (Exception e) {
		}
		return flag;
	}

	/***
	 * true:already in using false:not using
	 * 
	 * @param host
	 * @param port
	 * @throws UnknownHostException
	 */
	public static boolean isPortUsing(String host, int port) throws UnknownHostException {
		boolean flag = false;
		InetAddress theAddress = InetAddress.getByName(host);
		Socket socket = null;
		try {
			socket = new Socket(theAddress, port);
			flag = true;
		} catch (IOException e) {

		} finally {
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return flag;
	}
}
