- 论坛徽章:
- 0
|
各位前辈,我用
- Selector selector = null;
- try {
- // Create the selector
- selector = Selector.open();
- SocketChannel sChannel1 = createSocketChannel("hostname.com", 80);
- sChannel1.register(selector, sChannel1.validOps());
- } catch (IOException e) {
- }
-
- while (true) {
- try {
- selector.select(1000);
- } catch (IOException e) {
- break;
- }
-
- Iterator it = selector.selectedKeys().iterator();
- SelectionKey selKey = (SelectionKey)it.next();
- it.remove();
- processSelectionKey(selKey);
- }
-
- public void processSelectionKey(SelectionKey selKey) throws IOException {
- if (selKey.isValid() && selKey.isConnectable()) {
- // Get channel with connection request
- SocketChannel sChannel = (SocketChannel)selKey.channel();
-
- boolean success = sChannel.finishConnect();
- if (!success) {
- selKey.cancel();
- }
- }
- if (selKey.isValid() && selKey.isReadable()) {
- SocketChannel sChannel = (SocketChannel)selKey.channel();
- }
- }
- }
复制代码
这样select每秒都返回一次0,然后要等22秒才能返回一次1,才能到processSelectionKey函数实现socket连接?我早就在selector中注册了的,为啥每次都返回0 啊?。
什么情况下什么事件会让select返回1啊?
另外isConnectable什么时候会返回true?看文档说已经连接了就返回true,感觉不太是这样的、谢谢
[ 本帖最后由 HappyWin 于 2007-1-8 10:19 编辑 ] |
|