commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1080108 - in /commons/proper/net/trunk/src: changes/changes.xml main/java/org/apache/commons/net/SocketClient.java
Date Thu, 10 Mar 2011 02:30:07 GMT
Author: sebb
Date: Thu Mar 10 02:30:07 2011
New Revision: 1080108

URL: http://svn.apache.org/viewvc?rev=1080108&view=rev
Log:
NET-350 "java.net.SocketException: Broken pipe" when calling "TelnetClient.sendAYT()"
Added SocketClient#isAvailable() method to perform additional checks on a socket.

Modified:
    commons/proper/net/trunk/src/changes/changes.xml
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java

Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1080108&r1=1080107&r2=1080108&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml (original)
+++ commons/proper/net/trunk/src/changes/changes.xml Thu Mar 10 02:30:07 2011
@@ -57,6 +57,10 @@ The <action> type attribute can be add,u
 
     <body>
         <release version="3.0" date="TBA" description="TBA">
+            <action issue="NET-350" dev="sebb" type="add" due-to="Bogdan Drozdowski" due-to-email="bogdandr
# op . pl">
+            "java.net.SocketException: Broken pipe" when calling "TelnetClient.sendAYT()"
+            Added SocketClient#isAvailable() method to perform additional checks on a socket.
+            </action>
             <action issue="NET-237" dev="sebb" type="add">
             Add streaming methods (corresponding to array methods) to NNTPClient.
             </action>

Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java?rev=1080108&r1=1080107&r2=1080108&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java (original)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java Thu Mar
10 02:30:07 2011
@@ -325,6 +325,40 @@ public abstract class SocketClient
         return _socket_.isConnected();
     }
 
+    /**
+     * 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.
+     * @see <a href="https://issues.apache.org/jira/browse/NET-350">NET-350</a>
+     * @return {@code true} if the socket appears to be available for use
+     * @since 3.0
+     */
+    public boolean isAvailable(){
+        if (isConnected()) {
+            try
+            {
+                if (_socket_.getInetAddress() == null) return false;
+                if (_socket_.getPort() == 0) return false;
+                if (_socket_.getRemoteSocketAddress() == null) return false;
+                if (_socket_.isClosed()) return false;
+                /* 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: */
+                if (_socket_.isInputShutdown()) return false;
+                if (_socket_.isOutputShutdown()) return false;
+                /* ignore the result, catch exceptions: */
+                _socket_.getInputStream();
+                _socket_.getOutputStream();
+            }
+            catch (IOException ioex)
+            {
+                return false;
+            }            
+            return true;
+        } else {
+            return false;
+        }
+    }
 
     /**
      * Sets the default port the SocketClient should connect to when a port



Mime
View raw message