Java Socket Address Get stringToInetSocketAddress( String address, int defaultPort)

Here you can find the source of stringToInetSocketAddress( String address, int defaultPort)

Description

Converts a address in format "hostname:port" to a InetSocketAddress

License

BSD License

Parameter

Parameter Description
address String which represents the address in format "hostname:port".
defaultPort Port that is used if address String is just a hostname.

Declaration

static protected InetSocketAddress stringToInetSocketAddress(
        String address, int defaultPort) 

Method Source Code

//package com.java2s;
/*/*from w w  w.j  av a  2s  .c  om*/
 * Copyright (c) 2008-2011 by Paul Seiferth, Zuse Institute Berlin
 *
 * Licensed under the BSD License, see LICENSE file for details.
 *
 */

import java.net.InetSocketAddress;

public class Main {
    /**
     * Converts a address in format "hostname:port" to a InetSocketAddress
     * 
     * @param address
     *            String which represents the address in format "hostname:port".
     * @param defaultPort
     *            Port that is used if address String is just a hostname.
     */
    static protected InetSocketAddress stringToInetSocketAddress(
            String address, int defaultPort) {
        InetSocketAddress isa;
        int pos = 0;

        if ((pos = address.indexOf(':')) == -1) {
            isa = new InetSocketAddress(address, defaultPort);
        } else {
            isa = new InetSocketAddress(address.substring(0, pos),
                    Integer.parseInt(address.substring(pos + 1)));
        }
        return isa;
    }
}

Related

  1. sizeOfInet(InetSocketAddress inet)
  2. socketAddress2String(final SocketAddress addr)
  3. string2SocketAddress(final String addr)
  4. stringAddressToSocketAddress(String address)
  5. stringifyInetSocketAddressList(InetSocketAddress[] addresses)
  6. toAddressString(InetSocketAddress address)
  7. toByte(InetSocketAddress socketAddress)
  8. toBytes(SocketAddress address)
  9. toFileName(InetSocketAddress address)