Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  NIO help needed. Not selecting keys.  (Read 1291 times)
0 Members and 1 Guest are viewing this topic.
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Posted 2008-02-11 16:16:20 »

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:
1  
2  
3  
4  
5  
6  
7  
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:
1  
Connection established to {IP ADDRESS REMOVED} on port 21.


Now here is where I think the problem is:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
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?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-02-11 16:51:32 »

1  
2  
3  
while (!socketChannel.finishConnect()) {
  sleep(10);
}


As you are in non-blocking mode.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #2 - Posted 2008-02-11 17:34:44 »

Thanks a lot.  I think that did the trick. 

And yes I was in non-blocking mode, but I wasn't checking finnishConnect() for a false return.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (35 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (31 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!