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.Closeable;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.net.InetAddress;
25  import java.net.InetSocketAddress;
26  import java.net.Proxy;
27  import java.net.Socket;
28  import java.net.SocketException;
29  import java.nio.charset.Charset;
30  
31  import javax.net.ServerSocketFactory;
32  import javax.net.SocketFactory;
33  
34  /**
35   * The SocketClient provides the basic operations that are required of client objects accessing sockets. It is meant to be subclassed to avoid having to rewrite
36   * the same code over and over again to open a socket, close a socket, set timeouts, etc. Of special note is the {@link #setSocketFactory setSocketFactory }
37   * method, which allows you to control the type of Socket the SocketClient creates for initiating network connections. This is especially useful for adding SSL
38   * or proxy support as well as better support for applets. For example, you could create a {@link javax.net.SocketFactory} that requests browser security
39   * capabilities before creating a socket. All classes derived from SocketClient should use the {@link #_socketFactory_ _socketFactory_ } member variable to
40   * create Socket and ServerSocket instances rather than instantiating them by directly invoking a constructor. By honoring this contract you guarantee that a
41   * user will always be able to provide his own Socket implementations by substituting his own SocketFactory.
42   *
43   * @see SocketFactory
44   */
45  public abstract class SocketClient {
46      /**
47       * The end of line character sequence used by most IETF protocols. That is a carriage return followed by a newline: "\r\n"
48       */
49      public static final String NETASCII_EOL = "\r\n";
50  
51      /** The default SocketFactory shared by all SocketClient instances. */
52      private static final SocketFactory DEFAULT_SOCKET_FACTORY = SocketFactory.getDefault();
53  
54      /** The default {@link ServerSocketFactory} */
55      private static final ServerSocketFactory DEFAULT_SERVER_SOCKET_FACTORY = ServerSocketFactory.getDefault();
56  
57      /** The socket's connect timeout (0 = infinite timeout) */
58      private static final int DEFAULT_CONNECT_TIMEOUT = 60000;
59  
60      /**
61       * A ProtocolCommandSupport object used to manage the registering of ProtocolCommandListeners and the firing of ProtocolCommandEvents.
62       */
63      private ProtocolCommandSupport commandSupport;
64  
65      /** The timeout to use after opening a socket. */
66      protected int _timeout_;
67  
68      /** The socket used for the connection. */
69      protected Socket _socket_;
70  
71      /** The hostname used for the connection (null = no hostname supplied). */
72      protected String _hostname_;
73  
74      /** The remote socket address used for the connection. */
75      protected InetSocketAddress remoteInetSocketAddress;
76  
77      /** The default port the client should connect to. */
78      protected int _defaultPort_;
79  
80      /** The socket's InputStream. */
81      protected InputStream _input_;
82  
83      /** The socket's OutputStream. */
84      protected OutputStream _output_;
85  
86      /** The socket's SocketFactory. */
87      protected SocketFactory _socketFactory_;
88  
89      /** The socket's ServerSocket Factory. */
90      protected ServerSocketFactory _serverSocketFactory_;
91  
92      protected int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
93  
94      /** Hint for SO_RCVBUF size */
95      private int receiveBufferSize = -1;
96  
97      /** Hint for SO_SNDBUF size */
98      private int sendBufferSize = -1;
99  
100     /** The proxy to use when connecting. */
101     private Proxy connProxy;
102 
103     /**
104      * Charset to use for byte IO.
105      */
106     private Charset charset = Charset.defaultCharset();
107 
108     /**
109      * Default constructor for SocketClient. Initializes _socket_ to null, _timeout_ to 0, _defaultPort to 0, _isConnected_ to false, charset to
110      * {@code Charset.defaultCharset()} and _socketFactory_ to a shared instance of {@link org.apache.commons.net.DefaultSocketFactory}.
111      */
112     public SocketClient() {
113         _socket_ = null;
114         _hostname_ = null;
115         _input_ = null;
116         _output_ = null;
117         _timeout_ = 0;
118         _defaultPort_ = 0;
119         _socketFactory_ = DEFAULT_SOCKET_FACTORY;
120         _serverSocketFactory_ = DEFAULT_SERVER_SOCKET_FACTORY;
121     }
122 
123     // helper method to allow code to be shared with connect(String,...) methods
124     private void _connect(final InetSocketAddress remoteInetSocketAddress, final InetAddress localAddr, final int localPort) throws IOException {
125         this.remoteInetSocketAddress = remoteInetSocketAddress;
126         _socket_ = _socketFactory_.createSocket();
127         if (receiveBufferSize != -1) {
128             _socket_.setReceiveBufferSize(receiveBufferSize);
129         }
130         if (sendBufferSize != -1) {
131             _socket_.setSendBufferSize(sendBufferSize);
132         }
133         if (localAddr != null) {
134             _socket_.bind(new InetSocketAddress(localAddr, localPort));
135         }
136         _socket_.connect(remoteInetSocketAddress, connectTimeout);
137         _connectAction_();
138     }
139 
140     /**
141      * Because there are so many connect() methods, the _connectAction_() method is provided as a means of performing some action immediately after establishing
142      * a connection, rather than reimplementing all the connect() methods. The last action performed by every connect() method after opening a socket is to
143      * call this method.
144      * <p>
145      * This method sets the timeout on the just opened socket to the default timeout set by {@link #setDefaultTimeout setDefaultTimeout() }, sets _input_ and
146      * _output_ to the socket's InputStream and OutputStream respectively, and sets _isConnected_ to true.
147      * <p>
148      * Subclasses overriding this method should start by calling <code> super._connectAction_() </code> first to ensure the initialization of the aforementioned
149      * protected variables.
150      *
151      * @throws IOException (SocketException) if a problem occurs with the socket
152      */
153     protected void _connectAction_() throws IOException {
154         applySocketAttributes();
155         _input_ = _socket_.getInputStream();
156         _output_ = _socket_.getOutputStream();
157     }
158 
159     /**
160      * Adds a ProtocolCommandListener.
161      *
162      * @param listener The ProtocolCommandListener to add.
163      * @since 3.0
164      */
165     public void addProtocolCommandListener(final ProtocolCommandListener listener) {
166         getCommandSupport().addProtocolCommandListener(listener);
167     }
168 
169     /**
170      * Applies socket attributes.
171      *
172      * @throws SocketException if there is an error in the underlying protocol, such as a TCP error.
173      * @since 3.8.0
174      */
175     protected void applySocketAttributes() throws SocketException {
176         _socket_.setSoTimeout(_timeout_);
177     }
178 
179     private void closeQuietly(final Closeable close) {
180         if (close != null) {
181             try {
182                 close.close();
183             } catch (final IOException e) {
184                 // Ignored
185             }
186         }
187     }
188 
189     private void closeQuietly(final Socket socket) {
190         if (socket != null) {
191             try {
192                 socket.close();
193             } catch (final IOException e) {
194                 // Ignored
195             }
196         }
197     }
198 
199     /**
200      * Opens a Socket connected to a remote host at the current default port and originating from the current host at a system assigned port. Before returning,
201      * {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.
202      *
203      * @param host The remote host.
204      * @throws SocketException If the socket timeout could not be set.
205      * @throws IOException     If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from
206      *                         it.
207      */
208     public void connect(final InetAddress host) throws SocketException, IOException {
209         _hostname_ = null;
210         connect(host, _defaultPort_);
211     }
212 
213     /**
214      * Opens a Socket connected to a remote host at the specified port and originating from the current host at a system assigned port. Before returning,
215      * {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.
216      *
217      * @param host The remote host.
218      * @param port The port to connect to on the remote host.
219      * @throws SocketException If the socket timeout could not be set.
220      * @throws IOException     If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from
221      *                         it.
222      */
223     public void connect(final InetAddress host, final int port) throws SocketException, IOException {
224         _hostname_ = null;
225         _connect(new InetSocketAddress(host, port), null, -1);
226     }
227 
228     /**
229      * Opens a Socket connected to a remote host at the specified port and originating from the specified local address and port. Before returning,
230      * {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.
231      *
232      * @param host      The remote host.
233      * @param port      The port to connect to on the remote host.
234      * @param localAddr The local address to use.
235      * @param localPort The local port to use.
236      * @throws SocketException If the socket timeout could not be set.
237      * @throws IOException     If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from
238      *                         it.
239      */
240     public void connect(final InetAddress host, final int port, final InetAddress localAddr, final int localPort) throws SocketException, IOException {
241         _hostname_ = null;
242         _connect(new InetSocketAddress(host, port), localAddr, localPort);
243     }
244 
245     /**
246      * Opens a Socket connected to a remote host at the current default port and originating from the current host at a system assigned port. Before returning,
247      * {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.
248      *
249      * @param hostname The name of the remote host.
250      * @throws SocketException               If the socket timeout could not be set.
251      * @throws IOException                   If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is
252      *                                       derived from it.
253      * @throws java.net.UnknownHostException If the hostname cannot be resolved.
254      */
255     public void connect(final String hostname) throws SocketException, IOException {
256         connect(hostname, _defaultPort_);
257     }
258 
259     /**
260      * Opens a Socket connected to a remote host at the specified port and originating from the current host at a system assigned port. Before returning,
261      * {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.
262      *
263      * @param hostname The name of the remote host.
264      * @param port     The port to connect to on the remote host.
265      * @throws SocketException               If the socket timeout could not be set.
266      * @throws IOException                   If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is
267      *                                       derived from it.
268      * @throws java.net.UnknownHostException If the hostname cannot be resolved.
269      */
270     public void connect(final String hostname, final int port) throws SocketException, IOException {
271         connect(hostname, port, null, -1);
272     }
273 
274     /**
275      * Opens a Socket connected to a remote host at the specified port and originating from the specified local address and port. Before returning,
276      * {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.
277      *
278      * @param hostname  The name of the remote host.
279      * @param port      The port to connect to on the remote host.
280      * @param localAddr The local address to use.
281      * @param localPort The local port to use.
282      * @throws SocketException               If the socket timeout could not be set.
283      * @throws IOException                   If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is
284      *                                       derived from it.
285      * @throws java.net.UnknownHostException If the hostname cannot be resolved.
286      */
287     public void connect(final String hostname, final int port, final InetAddress localAddr, final int localPort) throws SocketException, IOException {
288         _hostname_ = hostname;
289         _connect(new InetSocketAddress(hostname, port), localAddr, localPort);
290     }
291 
292     /**
293      * Create the CommandSupport instance if required
294      */
295     protected void createCommandSupport() {
296         commandSupport = new ProtocolCommandSupport(this);
297     }
298 
299     /**
300      * Disconnects the socket connection. You should call this method after you've finished using the class instance and also before you call {@link #connect
301      * connect() } again. _isConnected_ is set to false, _socket_ is set to null, _input_ is set to null, and _output_ is set to null.
302      *
303      * @throws IOException If there is an error closing the socket.
304      */
305     public void disconnect() throws IOException {
306         closeQuietly(_socket_);
307         closeQuietly(_input_);
308         closeQuietly(_output_);
309         _socket_ = null;
310         _hostname_ = null;
311         _input_ = null;
312         _output_ = null;
313     }
314 
315     /**
316      * If there are any listeners, send them the command details.
317      *
318      * @param command the command name
319      * @param message the complete message, including command name
320      * @since 3.0
321      */
322     protected void fireCommandSent(final String command, final String message) {
323         if (getCommandSupport().getListenerCount() > 0) {
324             getCommandSupport().fireCommandSent(command, message);
325         }
326     }
327 
328     /**
329      * If there are any listeners, send them the reply details.
330      *
331      * @param replyCode the code extracted from the reply
332      * @param reply     the full reply text
333      * @since 3.0
334      */
335     protected void fireReplyReceived(final int replyCode, final String reply) {
336         if (getCommandSupport().getListenerCount() > 0) {
337             getCommandSupport().fireReplyReceived(replyCode, reply);
338         }
339     }
340 
341     /**
342      * Gets the charset.
343      *
344      * @return the charset.
345      * @since 3.3
346      */
347     public Charset getCharset() {
348         return charset;
349     }
350 
351     /**
352      * Gets the charset name.
353      *
354      * @return the charset.
355      * @since 3.3
356      * @deprecated Since the code now requires Java 1.6 as a minimum
357      */
358     @Deprecated
359     public String getCharsetName() {
360         return charset.name();
361     }
362 
363     /**
364      * Subclasses can override this if they need to provide their own instance field for backwards compatibility.
365      *
366      * @return the CommandSupport instance, may be {@code null}
367      * @since 3.0
368      */
369     protected ProtocolCommandSupport getCommandSupport() {
370         return commandSupport;
371     }
372 
373     /**
374      * Get the underlying socket connection timeout.
375      *
376      * @return timeout (in ms)
377      * @since 2.0
378      */
379     public int getConnectTimeout() {
380         return connectTimeout;
381     }
382 
383     /**
384      * Returns the current value of the default port (stored in {@link #_defaultPort_ _defaultPort_ }).
385      *
386      * @return The current value of the default port.
387      */
388     public int getDefaultPort() {
389         return _defaultPort_;
390     }
391 
392     /**
393      * Returns the default timeout in milliseconds that is used when opening a socket.
394      *
395      * @return The default timeout in milliseconds that is used when opening a socket.
396      */
397     public int getDefaultTimeout() {
398         return _timeout_;
399     }
400 
401     /**
402      * Returns the current value of the SO_KEEPALIVE flag on the currently opened socket. Delegates to {@link Socket#getKeepAlive()}
403      *
404      * @return True if SO_KEEPALIVE is enabled.
405      * @throws SocketException      if there is a problem with the socket
406      * @throws NullPointerException if the socket is not currently open
407      * @since 2.2
408      */
409     public boolean getKeepAlive() throws SocketException {
410         return _socket_.getKeepAlive();
411     }
412 
413     /**
414      * Returns the local address to which the client's socket is bound. Delegates to {@link Socket#getLocalAddress()}
415      *
416      * @return The local address to which the client's socket is bound.
417      * @throws NullPointerException if the socket is not currently open
418      */
419     public InetAddress getLocalAddress() {
420         return _socket_.getLocalAddress();
421     }
422 
423     /**
424      * Returns the port number of the open socket on the local host used for the connection. Delegates to {@link Socket#getLocalPort()}
425      *
426      * @return The port number of the open socket on the local host used for the connection.
427      * @throws NullPointerException if the socket is not currently open
428      */
429     public int getLocalPort() {
430         return _socket_.getLocalPort();
431     }
432 
433     /**
434      * Gets the proxy for use with all the connections.
435      *
436      * @return the current proxy for connections.
437      */
438     public Proxy getProxy() {
439         return connProxy;
440     }
441 
442     /**
443      * Get the current receivedBuffer size
444      *
445      * @return the size, or -1 if not initialized
446      * @since 3.0
447      */
448     protected int getReceiveBufferSize() {
449         return receiveBufferSize;
450     }
451 
452     /**
453      * @return The remote address to which the client is connected. Delegates to {@link Socket#getInetAddress()}
454      * @throws NullPointerException if the socket is not currently open
455      */
456     public InetAddress getRemoteAddress() {
457         return _socket_.getInetAddress();
458     }
459 
460     /**
461      * Gets the remote socket address used for the connection.
462      *
463      * @return the remote socket address used for the connection
464      * @since 3.10.0
465      */
466     protected InetSocketAddress getRemoteInetSocketAddress() {
467         return remoteInetSocketAddress;
468     }
469 
470     /**
471      * Returns the port number of the remote host to which the client is connected. Delegates to {@link Socket#getPort()}
472      *
473      * @return The port number of the remote host to which the client is connected.
474      * @throws NullPointerException if the socket is not currently open
475      */
476     public int getRemotePort() {
477         return _socket_.getPort();
478     }
479 
480     /**
481      * Get the current sendBuffer size
482      *
483      * @return the size, or -1 if not initialized
484      * @since 3.0
485      */
486     protected int getSendBufferSize() {
487         return sendBufferSize;
488     }
489 
490     /**
491      * Get the underlying {@link ServerSocketFactory}
492      *
493      * @return The server socket factory
494      * @since 2.2
495      */
496     public ServerSocketFactory getServerSocketFactory() {
497         return _serverSocketFactory_;
498     }
499 
500     /**
501      * Returns the current SO_LINGER timeout of the currently opened socket.
502      *
503      * @return The current SO_LINGER timeout. If SO_LINGER is disabled returns -1.
504      * @throws SocketException      If the operation fails.
505      * @throws NullPointerException if the socket is not currently open
506      */
507     public int getSoLinger() throws SocketException {
508         return _socket_.getSoLinger();
509     }
510 
511     /**
512      * Returns the timeout in milliseconds of the currently opened socket.
513      *
514      * @return The timeout in milliseconds of the currently opened socket.
515      * @throws SocketException      If the operation fails.
516      * @throws NullPointerException if the socket is not currently open
517      */
518     public int getSoTimeout() throws SocketException {
519         return _socket_.getSoTimeout();
520     }
521 
522     /**
523      * Returns true if Nagle's algorithm is enabled on the currently opened socket.
524      *
525      * @return True if Nagle's algorithm is enabled on the currently opened socket, false otherwise.
526      * @throws SocketException      If the operation fails.
527      * @throws NullPointerException if the socket is not currently open
528      */
529     public boolean getTcpNoDelay() throws SocketException {
530         return _socket_.getTcpNoDelay();
531     }
532 
533     /**
534      * Make various checks on the socket to test if it is available for use. Note that the only sure test is to use it, but these checks may help in some cases.
535      *
536      * @see <a href="https://issues.apache.org/jira/browse/NET-350">NET-350</a>
537      * @return {@code true} if the socket appears to be available for use
538      * @since 3.0
539      */
540     @SuppressWarnings("resource")
541     public boolean isAvailable() {
542         if (isConnected()) {
543             try {
544                 if (_socket_.getInetAddress() == null) {
545                     return false;
546                 }
547                 if (_socket_.getPort() == 0) {
548                     return false;
549                 }
550                 if (_socket_.getRemoteSocketAddress() == null) {
551                     return false;
552                 }
553                 if (_socket_.isClosed()) {
554                     return false;
555                 }
556                 /*
557                  * these aren't exact checks (a Socket can be half-open), but since we usually require two-way data transfer, we check these here too:
558                  */
559                 if (_socket_.isInputShutdown()) {
560                     return false;
561                 }
562                 if (_socket_.isOutputShutdown()) {
563                     return false;
564                 }
565                 /* ignore the result, catch exceptions: */
566                 // No need to close
567                 _socket_.getInputStream();
568                 // No need to close
569                 _socket_.getOutputStream();
570             } catch (final IOException ioex) {
571                 return false;
572             }
573             return true;
574         }
575         return false;
576     }
577 
578     /**
579      * Returns true if the client is currently connected to a server.
580      *
581      * Delegates to {@link Socket#isConnected()}
582      *
583      * @return True if the client is currently connected to a server, false otherwise.
584      */
585     public boolean isConnected() {
586         if (_socket_ == null) {
587             return false;
588         }
589 
590         return _socket_.isConnected();
591     }
592 
593     /**
594      * Removes a ProtocolCommandListener.
595      *
596      * @param listener The ProtocolCommandListener to remove.
597      * @since 3.0
598      */
599     public void removeProtocolCommandListener(final ProtocolCommandListener listener) {
600         getCommandSupport().removeProtocolCommandListener(listener);
601     }
602 
603     /**
604      * Sets the charset.
605      *
606      * @param charset the charset.
607      * @since 3.3
608      */
609     public void setCharset(final Charset charset) {
610         this.charset = charset;
611     }
612 
613     /**
614      * Sets the connection timeout in milliseconds, which will be passed to the {@link Socket} object's connect() method.
615      *
616      * @param connectTimeout The connection timeout to use (in ms)
617      * @since 2.0
618      */
619     public void setConnectTimeout(final int connectTimeout) {
620         this.connectTimeout = connectTimeout;
621     }
622 
623     /**
624      * Sets the default port the SocketClient should connect to when a port is not specified. The {@link #_defaultPort_ _defaultPort_ } variable stores this
625      * value. If never set, the default port is equal to zero.
626      *
627      * @param port The default port to set.
628      */
629     public void setDefaultPort(final int port) {
630         _defaultPort_ = port;
631     }
632 
633     /**
634      * Set the default timeout in milliseconds to use when opening a socket. This value is only used previous to a call to {@link #connect connect()} and should
635      * not be confused with {@link #setSoTimeout setSoTimeout()} which operates on the currently opened socket. _timeout_ contains the new timeout value.
636      *
637      * @param timeout The timeout in milliseconds to use for the socket connection.
638      */
639     public void setDefaultTimeout(final int timeout) {
640         _timeout_ = timeout;
641     }
642 
643     /**
644      * Sets the SO_KEEPALIVE flag on the currently opened socket.
645      *
646      * From the Javadocs, the default keepalive time is 2 hours (although this is implementation dependent). It looks as though the Windows WSA sockets
647      * implementation allows a specific keepalive value to be set, although this seems not to be the case on other systems.
648      *
649      * @param keepAlive If true, keepAlive is turned on
650      * @throws SocketException      if there is a problem with the socket
651      * @throws NullPointerException if the socket is not currently open
652      * @since 2.2
653      */
654     public void setKeepAlive(final boolean keepAlive) throws SocketException {
655         _socket_.setKeepAlive(keepAlive);
656     }
657 
658     /**
659      * Sets the proxy for use with all the connections. The proxy is used for connections established after the call to this method.
660      *
661      * @param proxy the new proxy for connections.
662      * @since 3.2
663      */
664     public void setProxy(final Proxy proxy) {
665         setSocketFactory(new DefaultSocketFactory(proxy));
666         connProxy = proxy;
667     }
668 
669     /**
670      * Sets the underlying socket receive buffer size.
671      *
672      * @param size The size of the buffer in bytes.
673      * @throws SocketException never (but subclasses may wish to do so)
674      * @since 2.0
675      */
676     public void setReceiveBufferSize(final int size) throws SocketException {
677         receiveBufferSize = size;
678     }
679 
680     /**
681      * Set the underlying socket send buffer size.
682      *
683      * @param size The size of the buffer in bytes.
684      * @throws SocketException never thrown, but subclasses might want to do so
685      * @since 2.0
686      */
687     public void setSendBufferSize(final int size) throws SocketException {
688         sendBufferSize = size;
689     }
690 
691     /**
692      * Sets the ServerSocketFactory used by the SocketClient to open ServerSocket connections. If the factory value is null, then a default factory is used
693      * (only do this to reset the factory after having previously altered it).
694      *
695      * @param factory The new ServerSocketFactory the SocketClient should use.
696      * @since 2.0
697      */
698     public void setServerSocketFactory(final ServerSocketFactory factory) {
699         if (factory == null) {
700             _serverSocketFactory_ = DEFAULT_SERVER_SOCKET_FACTORY;
701         } else {
702             _serverSocketFactory_ = factory;
703         }
704     }
705 
706     /**
707      * Sets the SocketFactory used by the SocketClient to open socket connections. If the factory value is null, then a default factory is used (only do this to
708      * reset the factory after having previously altered it). Any proxy setting is discarded.
709      *
710      * @param factory The new SocketFactory the SocketClient should use.
711      */
712     public void setSocketFactory(final SocketFactory factory) {
713         if (factory == null) {
714             _socketFactory_ = DEFAULT_SOCKET_FACTORY;
715         } else {
716             _socketFactory_ = factory;
717         }
718     }
719 
720     /**
721      * Sets the SO_LINGER timeout on the currently opened socket.
722      *
723      * @param on  True if linger is to be enabled, false if not.
724      * @param val The {@code linger} timeout (in hundredths of a second?)
725      * @throws SocketException      If the operation fails.
726      * @throws NullPointerException if the socket is not currently open
727      */
728     public void setSoLinger(final boolean on, final int val) throws SocketException {
729         _socket_.setSoLinger(on, val);
730     }
731 
732     /**
733      * Set the timeout in milliseconds of a currently open connection. Only call this method after a connection has been opened by {@link #connect connect()}.
734      * <p>
735      * To set the initial timeout, use {@link #setDefaultTimeout(int)} instead.
736      *
737      * @param timeout The timeout in milliseconds to use for the currently open socket connection.
738      * @throws SocketException      If the operation fails.
739      * @throws NullPointerException if the socket is not currently open
740      */
741     public void setSoTimeout(final int timeout) throws SocketException {
742         _socket_.setSoTimeout(timeout);
743     }
744 
745     /**
746      * Enables or disables the Nagle's algorithm (TCP_NODELAY) on the currently opened socket.
747      *
748      * @param on True if Nagle's algorithm is to be enabled, false if not.
749      * @throws SocketException      If the operation fails.
750      * @throws NullPointerException if the socket is not currently open
751      */
752     public void setTcpNoDelay(final boolean on) throws SocketException {
753         _socket_.setTcpNoDelay(on);
754     }
755 
756     /**
757      * Verifies that the remote end of the given socket is connected to the same host that the SocketClient is currently connected to. This is useful for
758      * doing a quick security check when a client needs to accept a connection from a server, such as an FTP data connection or a BSD R command standard error
759      * stream.
760      *
761      * @param socket the item to check against
762      * @return True if the remote hosts are the same, false if not.
763      */
764     public boolean verifyRemote(final Socket socket) {
765         final InetAddress host1;
766         final InetAddress host2;
767 
768         host1 = socket.getInetAddress();
769         host2 = getRemoteAddress();
770 
771         return host1.equals(host2);
772     }
773 
774     /*
775      * N.B. Fields cannot be pulled up into a super-class without breaking binary compatibility, so the abstract method is needed to pass the instance to the
776      * methods which were moved here.
777      */
778 }