Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PORT command while writing an FTP client with JAVA

Tags:

java

ftp

I am trying to write an FTP client using java.

As far as I understand I need to send my IP adress with PORT command when I need to establish an active connection but, How do I get that IP address?

I am trying to do something like this but it is obviously wrong:

ServerSocket ssock = new ServerSocket();
ssock.bind(null);
ssock.ssock.getLocalPort();
ssock.getInetAddress();

Last line returns null.

So again how can I send my IP address and port to server?

Thank you.

like image 427
slhsen Avatar asked Feb 15 '26 22:02

slhsen


1 Answers

You need to connect first, then call the socket.getLocalAddress() to get the correct local address. When you have multiple IPs, you only know the correct address to reach a specific destination after connection is established.

Sun JRE comes with a FTP client sun.net.ftp.FtpClient. You can check out how they do it.

With wide-spread use of NAT and proxies, PORT command rarely works. You should try passive mode first. As you can see, Sun's client only does PORT when PASV fails.

like image 150
ZZ Coder Avatar answered Feb 18 '26 13:02

ZZ Coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!