0
votes

Must the port number in my code be exactly the same one used by the FTP server (localhost, in this case) which I am attempting to connect to?

public class FTP {
    public static void main(String args[]) {
        FTPClient ftpc = new FTPClient();
        try {
            ftpc.connect("127.0.0.1",49961);
            ftpc.login("username", "password");
            System.out.println("Connected");
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("NOT Connected (socketERR) ");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("NOT Connected (ioERR) ");
        }

    } 
}

ERROR LOGS:

java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at org.apache.commons.net.SocketClient.connect(SocketClient.java:182) at org.apache.commons.net.SocketClient.connect(SocketClient.java:203) at com.testftp.FTP.main(FTP.java:23) NOT Connected (socketERR)

1
yes it must be exactly the same. Your server's ftp port is 21 or 49961? - M. Abbas
The only problem here is that you didn't get the port number right, and that isn't really a programming problem. - user207421
My port number is 49961. Simply i'm starting ftp server in cmd (ftp localhost) i'm reading port (cmd -> netstat) and writing it in my code, starting FTPClient and then i have this error. - Wojtek
It seem like a classic firewall issue. Please make sure the port is opened correctly in the firewall. - Ray 'user1578904'

1 Answers

0
votes

YES, it must.

By the way, this is valid for ANY service which attempts to connect to a server. Some services have established standard, default ports - FTP's is 21, for example. These default ports can be changed and the service run on alternative ports, as long as client and server both use the same port.