commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aha <aha1...@yahoo.com>
Subject Re: [net]FTPClient.connect() spend long time for "null" host
Date Fri, 18 Jun 2004 02:06:49 GMT
I implement class UtSocketFactory extends from class
DefaultSocketFactory, and FTPClient.setSocketFactory
for it. It has resolved the problem that I sumbit.



Thanks for Daniel's help!!!



The following is my code for new SocketFactory:
--------
UtSocketFactory utSocketFactory = new
UtSocketFactory();
utSocketFactory.setConnectTimeout(5000);
FTPClient ftp = new FTPClient();
ftp.setSocketFactory(utSocketFactory);
try {
	ftp.connect(server, port);
} catch (Exception e) {
	e.printStackTrace();
	if (ftp.isConnected()) {
		try {
			ftp.disconnect();
		} catch (Exception f) {
			// do nothing
		}
	}
	return RTN_CODE_FTP_SERVER_CONNECTION_ERROR;
}



The following is the class UtSocketFactory:
----------
public class UtSocketFactory extends
DefaultSocketFactory {
	private int connectTimeout = 0;
	/**
	 * 
	 */
	public UtSocketFactory() {
		super();
	}
	/* (non-Javadoc)
	 * @see
org.apache.commons.net.SocketFactory#createSocket(java.lang.String,
int)
	 */
	public Socket createSocket(String host, int port)
		throws UnknownHostException, IOException {
		Socket s = new java.net.Socket();
		java.net.InetSocketAddress addr =
			new java.net.InetSocketAddress(host, port);
		s.connect(addr, connectTimeout);
		return s;
	}
	/**
	 * @return
	 */
	public int getConnectTimeout() {
		return connectTimeout;
	}
	/**
	 * @param i
	 */
	public void setConnectTimeout(int i) {
		connectTimeout = i;
	}
}



--- "Daniel F. Savarese" <dfs@savarese.org> wrote:
> 
> In message
>
<20040615065124.69397.qmail@web11405.mail.yahoo.com>,
> aha writes:
> >When I modified the code as following, it looks
> that
> >resolved it.
> 
> Ideally, you would implement your own SocketFactory,
> perhaps by subclassing
> DefaultSocketFactory, and use setSocketFactory. 
> Otherwise, you have to
> modify the code every time there's a new Commons Net
> release.  Since
> Commons Net 1.2 is using J2SE 1.2 as a compatibility
> baseline, the Socket
> API additions that should have been in java.net from
> the start haven't
> been incorporated.
> 
> >To all: Is it correct??? Any comments are welcome!
> 
> Yes, because the new Socket.connect method allows
> you to override
> the connection timeout.  Previous to J2SE 1.4, you
> were stuck with
> the OS TCP stack default value and forced to use
> native code or play
> tricks with threads and timers to abort a connection
> attempt that
> was taking too long.
> 
> daniel



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail

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


Mime
View raw message