Answer the question
In order to leave comments, you need to log in
How to run a java program on centOS?
Hello, help me solve the problem,
There is computer 1 on it with os windows installed. On this computer, I wrote and assembled a simple java program in netbins. The essence of the program is to connect to a remote server via ftp and display a list of files on the screen. Here is the program code:
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.ftp.FTPClient;
public class Main {
public static void main(String[] args){
try {
FTPClient ftp = new FTPClient();
ftp.connect("host");
ftp.setDefaultPort(21);
ftp.enterLocalPassiveMode();
ftp.login("user","pass");
String[] list = ftp.listNames();
for(int i=0; i<list.length; i++){
System.out.println(list[i]);
}
ftp.logout();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
java -jar file.jar
Apr 02, 2015 11:19:40 AM Main main
SEVERE: null
java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:894)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:759)
at org.apache.commons.net.ftp.FTPClient.listNames(FTPClient.java:2825)
at org.apache.commons.net.ftp.FTPClient.listNames(FTPClient.java:2876)
at Main.main(Main.java:21)
Answer the question
In order to leave comments, you need to log in
The FTP protocol uses more than one port.
21 is the "Control Connection".
To transfer data (listing also applies to this) a separate connection is used, which is called "Data Connection".
In active mode, this connection is initiated by the FTP server. Which address and port to connect to - the client informs when opening a connection. This transfer mode is currently not recommended.
In passive mode, the connection is initiated by the client, and the address and port (dynamic, changing) are reported by the FTP server.
You need to enable more detailed logging in the FTP client so that the commands sent to the FTP server and the responses received are displayed.
Well, check if the connection to ports other than the 21st is forbidden somewhere.
On linux, ports 0-1000 can only be opened as root. Most likely this is the problem, you are simply not allowed to open the port. Try to run the code as root, if it works, then tune in.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question