View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.ServerSocket;
23  import java.net.Socket;
24  import java.net.UnknownHostException;
25  
26  import javax.net.SocketFactory;
27  
28  /***
29   * DefaultSocketFactory implements the SocketFactory interface by
30   * simply wrapping the java.net.Socket and java.net.ServerSocket
31   * constructors.  It is the default SocketFactory used by
32   * {@link org.apache.commons.net.SocketClient}
33   * implementations.
34   * <p>
35   * <p>
36   * @author Daniel F. Savarese
37   * @see SocketFactory
38   * @see SocketClient
39   * @see SocketClient#setSocketFactory
40   ***/
41  
42  public class DefaultSocketFactory extends SocketFactory
43  {
44  
45      /***
46       * Creates a Socket connected to the given host and port.
47       * <p>
48       * @param host The hostname to connect to.
49       * @param port The port to connect to.
50       * @return A Socket connected to the given host and port.
51       * @exception UnknownHostException  If the hostname cannot be resolved.
52       * @exception IOException If an I/O error occurs while creating the Socket.
53       ***/
54      @Override
55      public Socket createSocket(String host, int port)
56      throws UnknownHostException, IOException
57      {
58          return new Socket(host, port);
59      }
60  
61      /***
62       * Creates a Socket connected to the given host and port.
63       * <p>
64       * @param address The address of the host to connect to.
65       * @param port The port to connect to.
66       * @return A Socket connected to the given host and port.
67       * @exception IOException If an I/O error occurs while creating the Socket.
68       ***/
69      @Override
70      public Socket createSocket(InetAddress address, int port)
71      throws IOException
72      {
73          return new Socket(address, port);
74      }
75  
76      /***
77       * Creates a Socket connected to the given host and port and
78       * originating from the specified local address and port.
79       * <p>
80       * @param host The hostname to connect to.
81       * @param port The port to connect to.
82       * @param localAddr  The local address to use.
83       * @param localPort  The local port to use.
84       * @return A Socket connected to the given host and port.
85       * @exception UnknownHostException  If the hostname cannot be resolved.
86       * @exception IOException If an I/O error occurs while creating the Socket.
87       ***/
88      @Override
89      public Socket createSocket(String host, int port,
90                                 InetAddress localAddr, int localPort)
91      throws UnknownHostException, IOException
92      {
93          return new Socket(host, port, localAddr, localPort);
94      }
95  
96      /***
97       * Creates a Socket connected to the given host and port and
98       * originating from the specified local address and port.
99       * <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 }