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 *      http://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 * DefaultDatagramSocketFactory implements the DatagramSocketFactory interface by simply wrapping the java.net.DatagramSocket constructors. It is the default
026 * DatagramSocketFactory used by {@link org.apache.commons.net.DatagramSocketClient} implementations.
027 *
028 *
029 * @see DatagramSocketFactory
030 * @see DatagramSocketClient
031 * @see DatagramSocketClient#setDatagramSocketFactory
032 */
033
034public class DefaultDatagramSocketFactory implements DatagramSocketFactory {
035
036    /**
037     * Creates a DatagramSocket on the local host at the first available port.
038     *
039     * @return a new DatagramSocket
040     * @throws SocketException If the socket could not be created.
041     */
042    @Override
043    public DatagramSocket createDatagramSocket() throws SocketException {
044        return new DatagramSocket();
045    }
046
047    /**
048     * Creates a DatagramSocket on the local host at a specified port.
049     *
050     * @param port The port to use for the socket.
051     * @return a new DatagramSocket
052     * @throws SocketException If the socket could not be created.
053     */
054    @Override
055    public DatagramSocket createDatagramSocket(final int port) throws SocketException {
056        return new DatagramSocket(port);
057    }
058
059    /**
060     * Creates a DatagramSocket at the specified address on the local host at a specified port.
061     *
062     * @param port  The port to use for the socket.
063     * @param laddr The local address to use.
064     * @return a new DatagramSocket
065     * @throws SocketException If the socket could not be created.
066     */
067    @Override
068    public DatagramSocket createDatagramSocket(final int port, final InetAddress laddr) throws SocketException {
069        return new DatagramSocket(port, laddr);
070    }
071}