I am trying to learn NIO. In an effort to do so, I thought I would try and build an FTP client. So fro it is working not to bad, but I have run into a glitch. When I connect to a server that I have running on my machine, it connects just fine. When I try to connect to another FTP server, it hangs. I think I have identified the problem, but have no solution in mind. I will paste the code I think is relevent, but the full source is at http://www.myjavaserver.com/~captainjester/jftp.zip if you want to try it out. It is not complete yet, but it should connect, enter the userid and password, set the data port and ask for the file list(although my list parser is no working properly yet) the log will show what is happening.
This is what I get with my local FTP server:
Connection established to {IP ADDRESS REMOVED} on port 21.
220 Golden FTP Server ready v4.00b
331 User name okay, need password.
230 User logged in, proceed.
200 PORT Command successful.
125 File status okay; about to open data connection.
226 Closing data connection.
The connected line is generated by my program, but everything else is a response from the server.
When I try to connect to any other FTP I get this:
Connection established to {IP ADDRESS REMOVED} on port 21.
Now here is where I think the problem is:
public class Connection implements Runnable {
.
.
.
public void run() {
while(connected) {
try {
if(selector != null) {
int read = selector.select(20);
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while(it.hasNext()) {
SelectionKey tempKey = it.next();
it.remove();
if(tempKey.isValid()) {
if(tempKey.isReadable()) {
read(tempKey);
}
else if(tempKey.isWritable()) {
synchronized (LOCK) {
okToWrite = true;
}
}
}
}
if(key.isReadable()) {
read(key);
}
}
}
catch(IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(50);
Thread.yield();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
I have stepped through this code with both servers and have found that when I try to connect to an external FTP no key gets selected as available to use. But it does get selected when I get connected to my local server. I have also tried other FTP clients and the servers I try to connect to are working fine.
Here is my connection code so you can see I am setting OP_READ for the key.
public class Connection implements Runnable {
.
.
.
public void connect(String server, int port) throws UnknownHostException, IOException {
StringBuffer serverResponseCode = new StringBuffer();
try {
selector = SelectorProvider.provider().openSelector();
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress(server, port));
socketChannel.register(selector, SelectionKey.OP_READ);
socketChannel.finishConnect();
connected = true;
key = socketChannel.keyFor(selector);
mainPanel.addMessage("Connection established to " + server + " on port 21." + Resources.LINE_SEPARATOR);
Thread t = new Thread(this);
t.start();
getServerMessage(serverResponseCode);
if(!serverResponseCode.toString().equals("220")) {
disconnect();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
Any thoughts?