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: * Creates a Socket connected to the given host and port.
042: * <p>
043: * @param host The hostname to connect to.
044: * @param port The port to connect to.
045: * @return A Socket connected to the given host and port.
046: * @exception UnknownHostException If the hostname cannot be resolved.
047: * @exception IOException If an I/O error occurs while creating the Socket.
048: ***/
049: public Socket createSocket(String host, int port)
050: throws UnknownHostException, IOException {
051: return new Socket(host, port);
052: }
053:
054: /***
055: * Creates a Socket connected to the given host and port.
056: * <p>
057: * @param address The address of the host to connect to.
058: * @param port The port to connect to.
059: * @return A Socket connected to the given host and port.
060: * @exception IOException If an I/O error occurs while creating the Socket.
061: ***/
062: public Socket createSocket(InetAddress address, int port)
063: throws IOException {
064: return new Socket(address, port);
065: }
066:
067: /***
068: * Creates a Socket connected to the given host and port and
069: * originating from the specified local address and port.
070: * <p>
071: * @param host The hostname to connect to.
072: * @param port The port to connect to.
073: * @param localAddr The local address to use.
074: * @param localPort The local port to use.
075: * @return A Socket connected to the given host and port.
076: * @exception UnknownHostException If the hostname cannot be resolved.
077: * @exception IOException If an I/O error occurs while creating the Socket.
078: ***/
079: public Socket createSocket(String host, int port,
080: InetAddress localAddr, int localPort)
081: throws UnknownHostException, IOException {
082: return new Socket(host, port, localAddr, localPort);
083: }
084:
085: /***
086: * Creates a Socket connected to the given host and port and
087: * originating from the specified local address and port.
088: * <p>
089: * @param address The address of the host to connect to.
090: * @param port The port to connect to.
091: * @param localAddr The local address to use.
092: * @param localPort The local port to use.
093: * @return A Socket connected to the given host and port.
094: * @exception IOException If an I/O error occurs while creating the Socket.
095: ***/
096: public Socket createSocket(InetAddress address, int port,
097: InetAddress localAddr, int localPort) throws IOException {
098: return new Socket(address, port, localAddr, localPort);
099: }
100:
101: /***
102: * Creates a ServerSocket bound to a specified port. A port
103: * of 0 will create the ServerSocket on a system-determined free port.
104: * <p>
105: * @param port The port on which to listen, or 0 to use any free port.
106: * @return A ServerSocket that will listen on a specified port.
107: * @exception IOException If an I/O error occurs while creating
108: * the ServerSocket.
109: ***/
110: public ServerSocket createServerSocket(int port) throws IOException {
111: return new ServerSocket(port);
112: }
113:
114: /***
115: * Creates a ServerSocket bound to a specified port with a given
116: * maximum queue length for incoming connections. A port of 0 will
117: * 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: * @param backlog The maximum length of the queue for incoming connections.
121: * @return A ServerSocket that will listen on a specified port.
122: * @exception IOException If an I/O error occurs while creating
123: * the ServerSocket.
124: ***/
125: public ServerSocket createServerSocket(int port, int backlog)
126: throws IOException {
127: return new ServerSocket(port, backlog);
128: }
129:
130: /***
131: * Creates a ServerSocket bound to a specified port on a given local
132: * address with a given maximum queue length for incoming connections.
133: * A port of 0 will
134: * create the ServerSocket on a system-determined free port.
135: * <p>
136: * @param port The port on which to listen, or 0 to use any free port.
137: * @param backlog The maximum length of the queue for incoming connections.
138: * @param bindAddr The local address to which the ServerSocket should bind.
139: * @return A ServerSocket that will listen on a specified port.
140: * @exception IOException If an I/O error occurs while creating
141: * the ServerSocket.
142: ***/
143: public ServerSocket createServerSocket(int port, int backlog,
144: InetAddress bindAddr) throws IOException {
145: return new ServerSocket(port, backlog, bindAddr);
146: }
147: }
|