1. /*
  2. * Copyright 2001-2004 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. import java.io.IOException;
  18. import java.net.InetAddress;
  19. import java.net.ServerSocket;
  20. import java.net.Socket;
  21. import java.net.UnknownHostException;
  22. /***
  23. * DefaultSocketFactory implements the SocketFactory interface by
  24. * simply wrapping the java.net.Socket and java.net.ServerSocket
  25. * constructors. It is the default SocketFactory used by
  26. * <a href="org.apache.commons.net.SocketClient.html"> SocketClient </a>
  27. * implementations.
  28. * <p>
  29. * <p>
  30. * @author Daniel F. Savarese
  31. * @see SocketFactory
  32. * @see SocketClient
  33. * @see SocketClient#setSocketFactory
  34. ***/
  35. public class DefaultSocketFactory implements SocketFactory
  36. {
  37. /***
  38. * Creates a Socket connected to the given host and port.
  39. * <p>
  40. * @param host The hostname to connect to.
  41. * @param port The port to connect to.
  42. * @return A Socket connected to the given host and port.
  43. * @exception UnknownHostException If the hostname cannot be resolved.
  44. * @exception IOException If an I/O error occurs while creating the Socket.
  45. ***/
  46. public Socket createSocket(String host, int port)
  47. throws UnknownHostException, IOException
  48. {
  49. return new Socket(host, port);
  50. }
  51. /***
  52. * Creates a Socket connected to the given host and port.
  53. * <p>
  54. * @param address The address of the host to connect to.
  55. * @param port The port to connect to.
  56. * @return A Socket connected to the given host and port.
  57. * @exception IOException If an I/O error occurs while creating the Socket.
  58. ***/
  59. public Socket createSocket(InetAddress address, int port)
  60. throws IOException
  61. {
  62. return new Socket(address, port);
  63. }
  64. /***
  65. * Creates a Socket connected to the given host and port and
  66. * originating from the specified local address and port.
  67. * <p>
  68. * @param host The hostname to connect to.
  69. * @param port The port to connect to.
  70. * @param localAddr The local address to use.
  71. * @param localPort The local port to use.
  72. * @return A Socket connected to the given host and port.
  73. * @exception UnknownHostException If the hostname cannot be resolved.
  74. * @exception IOException If an I/O error occurs while creating the Socket.
  75. ***/
  76. public Socket createSocket(String host, int port,
  77. InetAddress localAddr, int localPort)
  78. throws UnknownHostException, IOException
  79. {
  80. return new Socket(host, port, localAddr, localPort);
  81. }
  82. /***
  83. * Creates a Socket connected to the given host and port and
  84. * originating from the specified local address and port.
  85. * <p>
  86. * @param address The address of the host to connect to.
  87. * @param port The port to connect to.
  88. * @param localAddr The local address to use.
  89. * @param localPort The local port to use.
  90. * @return A Socket connected to the given host and port.
  91. * @exception IOException If an I/O error occurs while creating the Socket.
  92. ***/
  93. public Socket createSocket(InetAddress address, int port,
  94. InetAddress localAddr, int localPort)
  95. throws IOException
  96. {
  97. return new Socket(address, port, localAddr, localPort);
  98. }
  99. /***
  100. * Creates a ServerSocket bound to a specified port. A port
  101. * of 0 will create the ServerSocket on a system-determined free port.
  102. * <p>
  103. * @param port The port on which to listen, or 0 to use any free port.
  104. * @return A ServerSocket that will listen on a specified port.
  105. * @exception IOException If an I/O error occurs while creating
  106. * the ServerSocket.
  107. ***/
  108. public ServerSocket createServerSocket(int port) throws IOException
  109. {
  110. return new ServerSocket(port);
  111. }
  112. /***
  113. * Creates a ServerSocket bound to a specified port with a given
  114. * maximum queue length for incoming connections. A port of 0 will
  115. * create the ServerSocket on a system-determined free port.
  116. * <p>
  117. * @param port The port on which to listen, or 0 to use any free port.
  118. * @param backlog The maximum length of the queue for incoming connections.
  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, int backlog)
  124. throws IOException
  125. {
  126. return new ServerSocket(port, backlog);
  127. }
  128. /***
  129. * Creates a ServerSocket bound to a specified port on a given local
  130. * address with a given maximum queue length for incoming connections.
  131. * 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. * @param bindAddr The local address to which the ServerSocket should bind.
  137. * @return A ServerSocket that will listen on a specified port.
  138. * @exception IOException If an I/O error occurs while creating
  139. * the ServerSocket.
  140. ***/
  141. public ServerSocket createServerSocket(int port, int backlog,
  142. InetAddress bindAddr)
  143. throws IOException
  144. {
  145. return new ServerSocket(port, backlog, bindAddr);
  146. }
  147. }