DefaultSocketFactory.java

  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. package org.apache.commons.net;

  18. import java.io.IOException;
  19. import java.net.InetAddress;
  20. import java.net.InetSocketAddress;
  21. import java.net.Proxy;
  22. import java.net.ServerSocket;
  23. import java.net.Socket;
  24. import java.net.UnknownHostException;

  25. import javax.net.SocketFactory;

  26. /**
  27.  * DefaultSocketFactory implements the SocketFactory interface by simply wrapping the java.net.Socket and java.net.ServerSocket constructors. It is the default
  28.  * SocketFactory used by {@link org.apache.commons.net.SocketClient} implementations.
  29.  *
  30.  *
  31.  * @see SocketFactory
  32.  * @see SocketClient
  33.  * @see SocketClient#setSocketFactory
  34.  */

  35. public class DefaultSocketFactory extends SocketFactory {
  36.     /** The proxy to use when creating new sockets. */
  37.     private final Proxy connProxy;

  38.     /**
  39.      * The default constructor.
  40.      */
  41.     public DefaultSocketFactory() {
  42.         this(null);
  43.     }

  44.     /**
  45.      * A constructor for sockets with proxy support.
  46.      *
  47.      * @param proxy The Proxy to use when creating new Sockets.
  48.      * @since 3.2
  49.      */
  50.     public DefaultSocketFactory(final Proxy proxy) {
  51.         connProxy = proxy;
  52.     }

  53.     /**
  54.      * Creates a ServerSocket bound to a specified port. A port of 0 will create the ServerSocket on a system-determined free port.
  55.      *
  56.      * @param port The port on which to listen, or 0 to use any free port.
  57.      * @return A ServerSocket that will listen on a specified port.
  58.      * @throws IOException If an I/O error occurs while creating the ServerSocket.
  59.      */
  60.     public ServerSocket createServerSocket(final int port) throws IOException {
  61.         return new ServerSocket(port);
  62.     }

  63.     /**
  64.      * Creates a ServerSocket bound to a specified port with a given maximum queue length for incoming connections. A port of 0 will create the ServerSocket on
  65.      * a system-determined free port.
  66.      *
  67.      * @param port    The port on which to listen, or 0 to use any free port.
  68.      * @param backlog The maximum length of the queue for incoming connections.
  69.      * @return A ServerSocket that will listen on a specified port.
  70.      * @throws IOException If an I/O error occurs while creating the ServerSocket.
  71.      */
  72.     public ServerSocket createServerSocket(final int port, final int backlog) throws IOException {
  73.         return new ServerSocket(port, backlog);
  74.     }

  75.     /**
  76.      * Creates a ServerSocket bound to a specified port on a given local address with a given maximum queue length for incoming connections. A port of 0 will
  77.      * create the ServerSocket on a system-determined free port.
  78.      *
  79.      * @param port     The port on which to listen, or 0 to use any free port.
  80.      * @param backlog  The maximum length of the queue for incoming connections.
  81.      * @param bindAddr The local address to which the ServerSocket should bind.
  82.      * @return A ServerSocket that will listen on a specified port.
  83.      * @throws IOException If an I/O error occurs while creating the ServerSocket.
  84.      */
  85.     public ServerSocket createServerSocket(final int port, final int backlog, final InetAddress bindAddr) throws IOException {
  86.         return new ServerSocket(port, backlog, bindAddr);
  87.     }

  88.     /**
  89.      * Creates an unconnected Socket.
  90.      *
  91.      * @return A new unconnected Socket.
  92.      * @throws IOException If an I/O error occurs while creating the Socket.
  93.      * @since 3.2
  94.      */
  95.     @Override
  96.     public Socket createSocket() throws IOException {
  97.         if (connProxy != null) {
  98.             return new Socket(connProxy);
  99.         }
  100.         return new Socket();
  101.     }

  102.     /**
  103.      * Creates a Socket connected to the given host and port.
  104.      *
  105.      * @param address The address of the host to connect to.
  106.      * @param port    The port to connect to.
  107.      * @return A Socket connected to the given host and port.
  108.      * @throws IOException If an I/O error occurs while creating the Socket.
  109.      */
  110.     @Override
  111.     public Socket createSocket(final InetAddress address, final int port) throws IOException {
  112.         if (connProxy != null) {
  113.             final Socket s = new Socket(connProxy);
  114.             s.connect(new InetSocketAddress(address, port));
  115.             return s;
  116.         }
  117.         return new Socket(address, port);
  118.     }

  119.     /**
  120.      * Creates a Socket connected to the given host and port and originating from the specified local address and port.
  121.      *
  122.      * @param address   The address of the host to connect to.
  123.      * @param port      The port to connect to.
  124.      * @param localAddr The local address to use.
  125.      * @param localPort The local port to use.
  126.      * @return A Socket connected to the given host and port.
  127.      * @throws IOException If an I/O error occurs while creating the Socket.
  128.      */
  129.     @Override
  130.     public Socket createSocket(final InetAddress address, final int port, final InetAddress localAddr, final int localPort) throws IOException {
  131.         if (connProxy != null) {
  132.             final Socket s = new Socket(connProxy);
  133.             s.bind(new InetSocketAddress(localAddr, localPort));
  134.             s.connect(new InetSocketAddress(address, port));
  135.             return s;
  136.         }
  137.         return new Socket(address, port, localAddr, localPort);
  138.     }

  139.     /**
  140.      * Creates a Socket connected to the given host and port.
  141.      *
  142.      * @param host The hostname to connect to.
  143.      * @param port The port to connect to.
  144.      * @return A Socket connected to the given host and port.
  145.      * @throws UnknownHostException If the hostname cannot be resolved.
  146.      * @throws IOException          If an I/O error occurs while creating the Socket.
  147.      */
  148.     @Override
  149.     public Socket createSocket(final String host, final int port) throws UnknownHostException, IOException {
  150.         if (connProxy != null) {
  151.             final Socket s = new Socket(connProxy);
  152.             s.connect(new InetSocketAddress(host, port));
  153.             return s;
  154.         }
  155.         return new Socket(host, port);
  156.     }

  157.     /**
  158.      * Creates a Socket connected to the given host and port and originating from the specified local address and port.
  159.      *
  160.      * @param host      The hostname to connect to.
  161.      * @param port      The port to connect to.
  162.      * @param localAddr The local address to use.
  163.      * @param localPort The local port to use.
  164.      * @return A Socket connected to the given host and port.
  165.      * @throws UnknownHostException If the hostname cannot be resolved.
  166.      * @throws IOException          If an I/O error occurs while creating the Socket.
  167.      */
  168.     @Override
  169.     public Socket createSocket(final String host, final int port, final InetAddress localAddr, final int localPort) throws UnknownHostException, IOException {
  170.         if (connProxy != null) {
  171.             final Socket s = new Socket(connProxy);
  172.             s.bind(new InetSocketAddress(localAddr, localPort));
  173.             s.connect(new InetSocketAddress(host, port));
  174.             return s;
  175.         }
  176.         return new Socket(host, port, localAddr, localPort);
  177.     }
  178. }