集册 Java实例教程 分析字符串并返回InetSocketAddress。

分析字符串并返回InetSocketAddress。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

520
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
分析字符串并返回InetSocketAddress。

/*

 *           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.nowjava;//来自 时 代 J a v a 公 众 号


import java.net.InetSocketAddress;


public class Main {

    public static void main(String[] argv) throws Exception {

        String addrString = "nowjava.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

     */
     /*
     来 自*
      n o w j a v a . c o m
     */

    public static InetSocketAddress parseSocketAddress(String addrString,

            int defaultPort) {

        try {

            String div[] = addrString.split(":");

       
展开阅读全文