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