View Javadoc

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