Here you can find the source of get_IP_from_SocketAddress(String addr)
Parameter | Description |
---|---|
addr | a parameter |
public static String get_IP_from_SocketAddress(String addr)
//package com.java2s; /* Copyright (C) 2011 Marius C. Silaghi Author: Marius Silaghi: msilaghi@fit.edu Florida Tech, Human Decision Support Systems Laboratory //w w w .j av a2 s. c om This program is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation; either the current version of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ import java.util.regex.Pattern; import java.net.SocketAddress; public class Main { /** * Extracts simple IP ("10.0.0.102") from "host/10.0.0.102:54321" * @param addr * @return */ public static String get_IP_from_SocketAddress(String addr) { String val[] = addr.split(Pattern.quote("/")); if ((val == null) || (val.length == 0)) return null; String ip_port = val[val.length - 1]; String[] split = ip_port.split(Pattern.quote(":")); if (split.length == 0) return null; return split[0]; } /** * from sock addr * @param clientAddress * @return */ public static String get_IP_from_SocketAddress(SocketAddress clientAddress) { if (clientAddress == null) return null; String addr = clientAddress.toString(); if (addr == null) return null; return get_IP_from_SocketAddress(addr); } }