Java InetAddress createSocket(InetAddress address, int port, boolean fixedPort)

Here you can find the source of createSocket(InetAddress address, int port, boolean fixedPort)

Description

Opens a socket on a specific network interface, either on a fixed or a random port.

License

Open Source License

Parameter

Parameter Description
address a parameter
port a parameter
fixedPort a parameter

Return

The new socket or null

Declaration

public static DatagramSocket createSocket(InetAddress address, int port, boolean fixedPort) 

Method Source Code

//package com.java2s;
/**/*from   ww  w.  j a  va 2 s.co m*/
* 
* Copyright (C) 2004-2008 FhG Fokus
*
* This file is part of the FhG Fokus UPnP stack - an open source UPnP implementation
* with some additional features
*
* You can redistribute the FhG Fokus UPnP stack and/or modify it
* under the terms of the GNU General Public License Version 3 as published by
* the Free Software Foundation.
*
* For a license to use the FhG Fokus UPnP stack software under conditions
* other than those described here, or to purchase support for this
* software, please contact Fraunhofer FOKUS by e-mail at the following
* addresses:
*   upnpstack@fokus.fraunhofer.de
*
* The FhG Fokus UPnP stack 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, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Random;

public class Main {
    /**
     * Opens a socket on a specific network interface, either on a fixed or a random port.
     * 
     * @param address
     * @param port
     * @param fixedPort
     * 
     * @return The new socket or null
     */
    public static DatagramSocket createSocket(InetAddress address, int port, boolean fixedPort) {
        DatagramSocket result = null;
        if (fixedPort) {
            try {
                result = new DatagramSocket(new InetSocketAddress(address, port));
                result.setSoTimeout(50);
            } catch (Exception e) {
                return null;
            }
        } else {
            Random rdm = new Random();
            port = port + rdm.nextInt(1024);
            while (result == null && port < 65530) {
                try {
                    result = new DatagramSocket(new InetSocketAddress(address, port));
                    result.setSoTimeout(50);
                } catch (Exception e) {
                    result = null;
                    port++;
                }
            }
        }
        return result;
    }
}

Related

  1. convertLongToInetAddress(long address)
  2. createAbsoluteURL(InetAddress address, int localStreamPort, URI relativeOrNot)
  3. createDatagramSocket(InetAddress addr, int port)
  4. createInetSocketAddress(final InetAddress address)
  5. createResolved(InetAddress address, int port)
  6. doMatch(InetAddress inetAddress, String toMatchParam)
  7. findFreePort(InetAddress address, int start, int end)
  8. findUnusedPort(InetAddress address, int from, int to)
  9. findUnusedPort(InetAddress address, int low, int high)