集册 Java实例教程 验证端口值是否在范围(1)内

验证端口值是否在范围(1)内

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

458
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
验证端口值是否在范围内(1-65535)

/*

 *  JMule - Java file sharing client

 *  Copyright (C) 2007-2008 JMule team ( jmule@jmule.org / http://jmule.org )

 *

 *  Any parts of this program derived from other projects, or contributed

 *  by third-party developers are copyrighted by their respective authors.

 *

 *  This program 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 2

 *  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 General Public License

 *  along with this program; if not, write to the Free Software

 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

 *

 */

//package com.nowjava;
/*
N o w  J a v a  .   c o m 提 供
*/


public class Main {

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

        int port = 2;

        System.out.println(isPortInRange(port));

    }


    /**

     * Validates a port value if it is in range ( 1 - 65535 )

     * 

     * @param port the port to verify in int value. Unsigned short ports must be

     * converted to singned int to let this function work correctly.

     * @return true if the port is in range, false otherwise.

     */

    public static boolean isPortInRange(int port) {

        return (port & 0xFFFF0000) == 0 && port != 0;

    }


    public static boolean isPortInRange(
展开阅读全文