标签:import als scl turn 返回 except new try package
业务场景:判断socket server在否在线,在线返回true,不在线返回false
package com.thinkgem.wlw.modules.test.socketdemo; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.SocketTimeoutException; import java.net.UnknownHostException; /** * @Author zhouhe * @Date 2019/10/18 14:24 */ public class SocketUtils { /** * 判断某服务能否连通 * * @param host host * @param port port * @return boolean */ public static boolean isRunning(String host, int port) { Socket sClient = null; try { SocketAddress saAdd = new InetSocketAddress(host.trim(), port); sClient = new Socket(); sClient.connect(saAdd, 3000); //设置超时 3s } catch (UnknownHostException e) { return false; } catch (SocketTimeoutException e) { return false; } catch (IOException e) { return false; } catch (Exception e) { return false; } finally { try { if (sClient != null) { sClient.close(); } } catch (Exception e) { } } return true; } }
标签:import als scl turn 返回 except new try package
原文地址:https://www.cnblogs.com/zhouheblog/p/11698166.html