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

Java examples for Network:Socket

Description

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

Demo Code

/*/*  www  .ja va  2  s .  c  om*/
 *  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.java2s;

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(Integer port) {

        return isPortInRange(port.intValue());
    }

    public static boolean isPortInRange(String port) {

        return isPortInRange(Integer.parseInt(port));
    }
}

Related Tutorials