Java Socket Address Get getInetSocketAddress(String arg)

Here you can find the source of getInetSocketAddress(String arg)

Description

Get the InetSocketAddress corresponding to the FTP format of address

License

Open Source License

Parameter

Parameter Description
arg a parameter

Return

the InetSocketAddress or null if an error occurs

Declaration

public static InetSocketAddress getInetSocketAddress(String arg) 

Method Source Code

//package com.java2s;
/**//from   www. ja  v  a 2 s.c o  m
 * This file is part of Waarp Project.
 * 
 * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
 * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
 * 
 * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 * 
 * Waarp 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 General Public License along with Waarp . If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class Main {
    /**
     * Get the InetSocketAddress corresponding to the FTP format of address
     * 
     * @param arg
     * @return the InetSocketAddress or null if an error occurs
     */
    public static InetSocketAddress getInetSocketAddress(String arg) {
        String[] elements = arg.split(",");
        if (elements.length != 6) {
            return null;
        }
        byte[] address = new byte[4];
        int[] iElements = new int[6];
        for (int i = 0; i < 6; i++) {
            try {
                iElements[i] = Integer.parseInt(elements[i]);
            } catch (NumberFormatException e) {
                return null;
            }
            if (iElements[i] < 0 || iElements[i] > 255) {
                return null;
            }
        }
        for (int i = 0; i < 4; i++) {
            address[i] = (byte) iElements[i];
        }
        int port = iElements[4] << 8 | iElements[5];
        InetAddress inetAddress;
        try {
            inetAddress = InetAddress.getByAddress(address);
        } catch (UnknownHostException e) {
            return null;
        }
        return new InetSocketAddress(inetAddress, port);
    }
}

Related

  1. getHostString(InetSocketAddress socketAddress)
  2. getHostStringFromInetSocketAddress(InetSocketAddress addr)
  3. getHostStringWithoutNameLookup( InetSocketAddress inetSocketAddress)
  4. getInetSocketAddress(long addrAsInt32, int port)
  5. getInetSocketAddress(String addressPortStr, int defaultPort)
  6. getInetSocketAddress(String endpoint)
  7. getInetSocketAddress(String endpointURI, int defaultPort)
  8. getInetSocketAddress(String s, String mainHost)
  9. getInetSocketAddressFromStringStrict(String s)