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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With