Search code examples
sftpjschhung

JSch session.connect() hungs with CoreFTP


I have CoreFTP configured for localhost and the next code:

JSch.setLogger(new MyJschLogger()); //class for console output

Session session = jsch.getSession("user", "localhost", 21);

Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);

session.setPassword("password");
session.connect();

when program achieves connect(), two messages appear at console output:

INFO: Connecting to localhost port 21
INFO: Connection established

...and nothing more happens. After some minutes, connection is closed by foreign host exception appears.

Why?

Thanks for all!


Solution

  • Port 21 is the normal port for FTP. JSch is only an SSH client, with support for SFTP in the ChannelSFTP class. JSch knows nothing about FTP (and SFTP is unrelated to FTP, other than by name and that it allows similar things).

    You need to setup your server to use the SSH protocol (usually on port 22, but you can use any port, as long as you use the same port on the client). See the documentation - I think you have to check the SSH check box.

    Also, if your code is nothing more than what you posted, then nothing more than connecting will happen. To transfer files, you will need to open a ChannelSFTP, and issue the right commands (e.g. call one or more of the put/get methods).