001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net;
019
020import java.net.DatagramSocket;
021import java.net.InetAddress;
022import java.net.SocketException;
023
024/**
025 * Implements the DatagramSocketFactory interface by simply wrapping the {@link DatagramSocket} constructors. It is the default DatagramSocketFactory used by
026 * {@link org.apache.commons.net.DatagramSocketClient} implementations.
027 *
028 * @see DatagramSocketFactory
029 * @see DatagramSocketClient
030 * @see DatagramSocketClient#setDatagramSocketFactory
031 */
032public class DefaultDatagramSocketFactory implements DatagramSocketFactory {
033
034    /**
035     * Constructs a new instance.
036     */
037    public DefaultDatagramSocketFactory() {
038        // empty
039    }
040
041    /**
042     * Creates a DatagramSocket on the local host at the first available port.
043     *
044     * @return a new DatagramSocket
045     * @throws SocketException If the socket could not be created.
046     */
047    @Override
048    public DatagramSocket createDatagramSocket() throws SocketException {
049        return new DatagramSocket();
050    }
051
052    /**
053     * Creates a DatagramSocket on the local host at a specified port.
054     *
055     * @param port The port to use for the socket.
056     * @return a new DatagramSocket
057     * @throws SocketException If the socket could not be created.
058     */
059    @Override
060    public DatagramSocket createDatagramSocket(final int port) throws SocketException {
061        return new DatagramSocket(port);
062    }
063
064    /**
065     * Creates a DatagramSocket at the specified address on the local host at a specified port.
066     *
067     * @param port  The port to use for the socket.
068     * @param localAddress The local address to use.
069     * @return a new DatagramSocket
070     * @throws SocketException If the socket could not be created.
071     */
072    @Override
073    public DatagramSocket createDatagramSocket(final int port, final InetAddress localAddress) throws SocketException {
074        return new DatagramSocket(port, localAddress);
075    }
076}