001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.net;
017    
018    import java.io.IOException;
019    import java.net.InetAddress;
020    import java.net.ServerSocket;
021    import java.net.Socket;
022    import java.net.UnknownHostException;
023    
024    /***
025     * DefaultSocketFactory implements the SocketFactory interface by
026     * simply wrapping the java.net.Socket and java.net.ServerSocket
027     * constructors.  It is the default SocketFactory used by
028     * {@link org.apache.commons.net.SocketClient}
029     * implementations.
030     * <p>
031     * <p>
032     * @author Daniel F. Savarese
033     * @see SocketFactory
034     * @see SocketClient
035     * @see SocketClient#setSocketFactory
036     ***/
037    
038    public class DefaultSocketFactory implements SocketFactory
039    {
040    
041        /***
042         * Creates a Socket connected to the given host and port.
043         * <p>
044         * @param host The hostname to connect to.
045         * @param port The port to connect to.
046         * @return A Socket connected to the given host and port.
047         * @exception UnknownHostException  If the hostname cannot be resolved.
048         * @exception IOException If an I/O error occurs while creating the Socket.
049         ***/
050        public Socket createSocket(String host, int port)
051        throws UnknownHostException, IOException
052        {
053            return new Socket(host, port);
054        }
055    
056        /***
057         * Creates a Socket connected to the given host and port.
058         * <p>
059         * @param address The address of the host to connect to.
060         * @param port The port to connect to.
061         * @return A Socket connected to the given host and port.
062         * @exception IOException If an I/O error occurs while creating the Socket.
063         ***/
064        public Socket createSocket(InetAddress address, int port)
065        throws IOException
066        {
067            return new Socket(address, port);
068        }
069    
070        /***
071         * Creates a Socket connected to the given host and port and
072         * originating from the specified local address and port.
073         * <p>
074         * @param host The hostname to connect to.
075         * @param port The port to connect to.
076         * @param localAddr  The local address to use.
077         * @param localPort  The local port to use.
078         * @return A Socket connected to the given host and port.
079         * @exception UnknownHostException  If the hostname cannot be resolved.
080         * @exception IOException If an I/O error occurs while creating the Socket.
081         ***/
082        public Socket createSocket(String host, int port,
083                                   InetAddress localAddr, int localPort)
084        throws UnknownHostException, IOException
085        {
086            return new Socket(host, port, localAddr, localPort);
087        }
088    
089        /***
090         * Creates a Socket connected to the given host and port and
091         * originating from the specified local address and port.
092         * <p>
093         * @param address The address of the host to connect to.
094         * @param port The port to connect to.
095         * @param localAddr  The local address to use.
096         * @param localPort  The local port to use.
097         * @return A Socket connected to the given host and port.
098         * @exception IOException If an I/O error occurs while creating the Socket.
099         ***/
100        public Socket createSocket(InetAddress address, int port,
101                                   InetAddress localAddr, int localPort)
102        throws IOException
103        {
104            return new Socket(address, port, localAddr, localPort);
105        }
106    
107        /***
108         * Creates a ServerSocket bound to a specified port.  A port
109         * of 0 will create the ServerSocket on a system-determined free port.
110         * <p>
111         * @param port  The port on which to listen, or 0 to use any free port.
112         * @return A ServerSocket that will listen on a specified port.
113         * @exception IOException If an I/O error occurs while creating
114         *                        the ServerSocket.
115         ***/
116        public ServerSocket createServerSocket(int port) throws IOException
117        {
118            return new ServerSocket(port);
119        }
120    
121        /***
122         * Creates a ServerSocket bound to a specified port with a given
123         * maximum queue length for incoming connections.  A port of 0 will
124         * create the ServerSocket on a system-determined free port.
125         * <p>
126         * @param port  The port on which to listen, or 0 to use any free port.
127         * @param backlog  The maximum length of the queue for incoming connections.
128         * @return A ServerSocket that will listen on a specified port.
129         * @exception IOException If an I/O error occurs while creating
130         *                        the ServerSocket.
131         ***/
132        public ServerSocket createServerSocket(int port, int backlog)
133        throws IOException
134        {
135            return new ServerSocket(port, backlog);
136        }
137    
138        /***
139         * Creates a ServerSocket bound to a specified port on a given local
140         * address with a given maximum queue length for incoming connections.
141         * A port of 0 will
142         * create the ServerSocket on a system-determined free port.
143         * <p>
144         * @param port  The port on which to listen, or 0 to use any free port.
145         * @param backlog  The maximum length of the queue for incoming connections.
146         * @param bindAddr  The local address to which the ServerSocket should bind.
147         * @return A ServerSocket that will listen on a specified port.
148         * @exception IOException If an I/O error occurs while creating
149         *                        the ServerSocket.
150         ***/
151        public ServerSocket createServerSocket(int port, int backlog,
152                                               InetAddress bindAddr)
153        throws IOException
154        {
155            return new ServerSocket(port, backlog, bindAddr);
156        }
157    }