Java examples for Network:Network Address
Parses the string and returns a InetSocketAddress.
/*/*from ww w . j av a 2s .c o m*/ * GNU GENERAL PUBLIC LICENSE * Version 2, June 1991 * * * * ADDITIONAL REQUIREMENT * * 1. If PeerfactSim.KOM is used for the evaluation in research publications, * the following should be referenced: * * @techreport{ PWLS07, * author = {Konstantin Pussep and Matthias Weinert and Nicolas Liebau and Ralf Steinmetz}, * title = {Flexible Framework for NAT Traversal in Peer-to-Peer Applications}, * month = {Nov}, * year = {2007}, * institution = {KOM - Multimedia Communications Lab, Technische Universit{\"a}t Darmstadt}, * address = {Merckstra{\ss}e 25, 64283 Darmstadt, Germany}, * number = {KOM-TR-2007-06}, * } */ //package com.java2s; import java.net.InetSocketAddress; public class Main { public static void main(String[] argv) throws Exception { String addrString = "java2s.com"; System.out.println(parseSocketAddress(addrString)); } /** * Parses the string and returns a <tt>InetSocketAddress</tt>. Address must have the format * <tt>hostname:port</tt>. * * @param addrString a string of a hostname and port * @return corresponding InetSocketAddress */ public static InetSocketAddress parseSocketAddress(String addrString) { return parseSocketAddress(addrString, -1); } /** * Parses the string and returns a <tt>InetSocketAddress</tt>. Address can have the format * <tt>hostname:port</tt> or only <tt>hostname</tt>, then the <tt>defaultPort</tt> is used. * * @param addrString a string of a hostname and port * @return corresponding InetSocketAddress */ public static InetSocketAddress parseSocketAddress(String addrString, int defaultPort) { try { String div[] = addrString.split(":"); String hostname = div[0]; int port = defaultPort; if (div.length > 1) port = Integer.valueOf(div[1]); return new InetSocketAddress(hostname, port); } catch (Exception e) { return null; } } }