Java Socket Address Get get_IP_from_SocketAddress(String addr)

Here you can find the source of get_IP_from_SocketAddress(String addr)

Description

Extracts simple IP ("10.0.0.102") from "host/10.0.0.102:54321"

License

Open Source License

Parameter

Parameter Description
addr a parameter

Declaration

public static String get_IP_from_SocketAddress(String addr) 

Method Source Code

//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);
    }
}

Related

  1. formatInetAddr(InetSocketAddress addr)
  2. formatSocketAddress(final InetSocketAddress inetSocketAddress)
  3. formatSocketAddress(SocketAddress address)
  4. get2428Address(InetSocketAddress address)
  5. get2428InetSocketAddress(String arg)
  6. getAddress(InetSocketAddress address)
  7. getAddress(InetSocketAddress address)
  8. getAddress(SocketAddress address)
  9. getAddress(SocketAddress address)