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.io.IOException;
021import java.net.InetAddress;
022import java.net.ServerSocket;
023import java.net.Socket;
024import java.net.UnknownHostException;
025
026import javax.net.SocketFactory;
027
028/***
029 * DefaultSocketFactory implements the SocketFactory interface by
030 * simply wrapping the java.net.Socket and java.net.ServerSocket
031 * constructors.  It is the default SocketFactory used by
032 * {@link org.apache.commons.net.SocketClient}
033 * implementations.
034 * <p>
035 * <p>
036 * @author Daniel F. Savarese
037 * @see SocketFactory
038 * @see SocketClient
039 * @see SocketClient#setSocketFactory
040 ***/
041
042public class DefaultSocketFactory extends SocketFactory
043{
044
045    /***
046     * Creates a Socket connected to the given host and port.
047     * <p>
048     * @param host The hostname to connect to.
049     * @param port The port to connect to.
050     * @return A Socket connected to the given host and port.
051     * @exception UnknownHostException  If the hostname cannot be resolved.
052     * @exception IOException If an I/O error occurs while creating the Socket.
053     ***/
054    @Override
055    public Socket createSocket(String host, int port)
056    throws UnknownHostException, IOException
057    {
058        return new Socket(host, port);
059    }
060
061    /***
062     * Creates a Socket connected to the given host and port.
063     * <p>
064     * @param address The address of the host to connect to.
065     * @param port The port to connect to.
066     * @return A Socket connected to the given host and port.
067     * @exception IOException If an I/O error occurs while creating the Socket.
068     ***/
069    @Override
070    public Socket createSocket(InetAddress address, int port)
071    throws IOException
072    {
073        return new Socket(address, port);
074    }
075
076    /***
077     * Creates a Socket connected to the given host and port and
078     * originating from the specified local address and port.
079     * <p>
080     * @param host The hostname to connect to.
081     * @param port The port to connect to.
082     * @param localAddr  The local address to use.
083     * @param localPort  The local port to use.
084     * @return A Socket connected to the given host and port.
085     * @exception UnknownHostException  If the hostname cannot be resolved.
086     * @exception IOException If an I/O error occurs while creating the Socket.
087     ***/
088    @Override
089    public Socket createSocket(String host, int port,
090                               InetAddress localAddr, int localPort)
091    throws UnknownHostException, IOException
092    {
093        return new Socket(host, port, localAddr, localPort);
094    }
095
096    /***
097     * Creates a Socket connected to the given host and port and
098     * originating from the specified local address and port.
099     * <p>
100     * @param address The address of the host to connect to.
101     * @param port The port to connect to.
102     * @param localAddr  The local address to use.
103     * @param localPort  The local port to use.
104     * @return A Socket connected to the given host and port.
105     * @exception IOException If an I/O error occurs while creating the Socket.
106     ***/
107    @Override
108    public Socket createSocket(InetAddress address, int port,
109                               InetAddress localAddr, int localPort)
110    throws IOException
111    {
112        return new Socket(address, port, localAddr, localPort);
113    }
114
115    /***
116     * Creates a ServerSocket bound to a specified port.  A port
117     * of 0 will create the ServerSocket on a system-determined free port.
118     * <p>
119     * @param port  The port on which to listen, or 0 to use any free port.
120     * @return A ServerSocket that will listen on a specified port.
121     * @exception IOException If an I/O error occurs while creating
122     *                        the ServerSocket.
123     ***/
124    public ServerSocket createServerSocket(int port) throws IOException
125    {
126        return new ServerSocket(port);
127    }
128
129    /***
130     * Creates a ServerSocket bound to a specified port with a given
131     * maximum queue length for incoming connections.  A port of 0 will
132     * create the ServerSocket on a system-determined free port.
133     * <p>
134     * @param port  The port on which to listen, or 0 to use any free port.
135     * @param backlog  The maximum length of the queue for incoming connections.
136     * @return A ServerSocket that will listen on a specified port.
137     * @exception IOException If an I/O error occurs while creating
138     *                        the ServerSocket.
139     ***/
140    public ServerSocket createServerSocket(int port, int backlog)
141    throws IOException
142    {
143        return new ServerSocket(port, backlog);
144    }
145
146    /***
147     * Creates a ServerSocket bound to a specified port on a given local
148     * address with a given maximum queue length for incoming connections.
149     * A port of 0 will
150     * create the ServerSocket on a system-determined free port.
151     * <p>
152     * @param port  The port on which to listen, or 0 to use any free port.
153     * @param backlog  The maximum length of the queue for incoming connections.
154     * @param bindAddr  The local address to which the ServerSocket should bind.
155     * @return A ServerSocket that will listen on a specified port.
156     * @exception IOException If an I/O error occurs while creating
157     *                        the ServerSocket.
158     ***/
159    public ServerSocket createServerSocket(int port, int backlog,
160                                           InetAddress bindAddr)
161    throws IOException
162    {
163        return new ServerSocket(port, backlog, bindAddr);
164    }
165}