commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwins...@apache.org
Subject svn commit: r473763 - in /jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src: main/java/org/apache/commons/net/SocketClient.java test/java/org/apache/commons/net/ftp/TestConnectTimeout.java
Date Sat, 11 Nov 2006 17:46:12 GMT
Author: rwinston
Date: Sat Nov 11 09:46:12 2006
New Revision: 473763

URL: http://svn.apache.org/viewvc?view=rev&rev=473763
Log:
Add preliminary support for socket connection timeout

Added:
    jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/test/java/org/apache/commons/net/ftp/TestConnectTimeout.java
Modified:
    jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/SocketClient.java

Modified: jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/SocketClient.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/SocketClient.java?view=diff&rev=473763&r1=473762&r2=473763
==============================================================================
--- jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/SocketClient.java
(original)
+++ jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/SocketClient.java
Sat Nov 11 09:46:12 2006
@@ -19,8 +19,10 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.SocketException;
+import java.net.UnknownHostException;
 
 import javax.net.ServerSocketFactory;
 import javax.net.SocketFactory;
@@ -82,6 +84,10 @@
     
     /** The socket's ServerSocket Factory. */
     protected ServerSocketFactory _serverSocketFactory_;
+    
+    /** The socket's connect timeout (0 = infinite timeout) */
+    private static final int DEFAULT_CONNECT_TIMEOUT = 0;
+    protected int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
 
     /**
      * Default constructor for SocketClient.  Initializes
@@ -141,7 +147,12 @@
     public void connect(InetAddress host, int port)
     throws SocketException, IOException
     {
-        _socket_ = _socketFactory_.createSocket(host, port);
+    	_socket_ = _socketFactory_.createSocket();
+    	if (connectTimeout == DEFAULT_CONNECT_TIMEOUT)
+    		_socket_.connect(new InetSocketAddress(host, port));
+    	else 
+    		_socket_.connect(new InetSocketAddress(host, port), connectTimeout);
+
         _connectAction_();
     }
 
@@ -162,7 +173,12 @@
     public void connect(String hostname, int port)
     throws SocketException, IOException
     {
-        _socket_ = _socketFactory_.createSocket(hostname, port);
+    	_socket_= _socketFactory_.createSocket();
+    	if (connectTimeout == DEFAULT_CONNECT_TIMEOUT)
+    		_socket_.connect(new InetSocketAddress(hostname, port));
+    	else
+    		_socket_.connect(new InetSocketAddress(hostname, port), connectTimeout);
+    	
         _connectAction_();
     }
 
@@ -186,7 +202,14 @@
                         InetAddress localAddr, int localPort)
     throws SocketException, IOException
     {
-        _socket_ = _socketFactory_.createSocket(host, port, localAddr, localPort);
+    	_socket_ = _socketFactory_.createSocket();
+    	_socket_.bind(new InetSocketAddress(localAddr, localPort));
+    	
+    	if (connectTimeout == DEFAULT_CONNECT_TIMEOUT)
+    		_socket_.connect(new InetSocketAddress(host, port));
+    	else 
+    		_socket_.connect(new InetSocketAddress(host, port), connectTimeout);
+        
         _connectAction_();
     }
 
@@ -519,6 +542,26 @@
 		else
 			_serverSocketFactory_ = factory;
 	}
+    
+    /**
+     * Sets the connection timeout in milliseconds, which will be passed to the {@link Socket}
object's
+     * connect() method. 
+     * @param connectTimeout The connection timeout to use (in ms)
+     */
+    public void setConnectTimeout(int connectTimeout) {
+    	this.connectTimeout = connectTimeout;
+    }
+    
+    /**
+     * Get the underlying socket connection timeout.
+     * @return
+     */
+    public int getConnectTimeout() {
+    	return connectTimeout;
+    }
+    
+    
+    
 }
 
 

Added: jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/test/java/org/apache/commons/net/ftp/TestConnectTimeout.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/test/java/org/apache/commons/net/ftp/TestConnectTimeout.java?view=auto&rev=473763
==============================================================================
--- jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/test/java/org/apache/commons/net/ftp/TestConnectTimeout.java
(added)
+++ jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/test/java/org/apache/commons/net/ftp/TestConnectTimeout.java
Sat Nov 11 09:46:12 2006
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2001-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp;
+
+import java.io.IOException;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+
+import junit.framework.TestCase;
+
+/**
+ * Test the socket connect timeout functionality
+ * @author Rory <rwinston@apache.org>
+ *
+ */
+public class TestConnectTimeout extends TestCase {
+
+	public void testConnectTimeout() throws SocketException, IOException {
+		FTPClient client = new FTPClient();
+		client.setConnectTimeout(1000);
+		
+		try {
+			// Connect to a valid host on a bogus port
+			client.connect("ftp.microsoft.com", 1234);
+			assertTrue("Expecting SocketTImeoutException", false);
+		} 
+		catch (SocketTimeoutException se) {
+			assertTrue(true);
+		}
+		
+	}
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message