当前位置: 首页>>代码示例>>Java>>正文


Java SocketChannel.finishConnect方法代码示例

本文整理汇总了Java中java.nio.channels.SocketChannel.finishConnect方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.finishConnect方法的具体用法?Java SocketChannel.finishConnect怎么用?Java SocketChannel.finishConnect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.SocketChannel的用法示例。


在下文中一共展示了SocketChannel.finishConnect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: start0

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void start0() throws IOException {
    while (!closed) {

        processQueues();
        selector.select();

        if (selector.selectedKeys().isEmpty()) {
            processQueues();
        }

        for (Iterator<SelectionKey> i = selector.selectedKeys().iterator(); i.hasNext(); ) {
            SelectionKey key = i.next();
            i.remove();

            if (key.isConnectable()) {
                SocketChannel socketChannel = (SocketChannel) key.channel();
                socketChannel.finishConnect();
                LOG.info("SocketChannel connected.");
            }

            if (key.isReadable()) {
                this.socketChannelReader.readFromKey(key);
            }
        }
    }
}
 
开发者ID:yiding-he,项目名称:java-nio-test,代码行数:27,代码来源:SocketChannelClientHandler.java

示例2: openSocketChannel

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public synchronized SocketChannel openSocketChannel(Address address) throws IOException {

        if (this.clientSocketChannel != null && this.clientSocketChannel.isOpen()) {
            return this.clientSocketChannel;
        }

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);

        this.registerPendingMap.put(socketChannel, OP_CONNECT | OP_READ);
        this.selector.wakeup();

        LOG.info("Connecting to " + address + "...");
        socketChannel.connect(new InetSocketAddress(address.getHost(), address.getPort()));

        while (!socketChannel.finishConnect()) {

        }
        LOG.info("Connected.");

        this.clientSocketChannel = socketChannel;
        return this.clientSocketChannel;
    }
 
开发者ID:yiding-he,项目名称:java-nio-test,代码行数:24,代码来源:SocketChannelClientHandler.java

示例3: testConnectCallback

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Test
public void testConnectCallback() throws IOException {
    eventLoop.registerAccept(acceptSocket, serverHandler);

    SocketChannel clientSocket = SocketChannel.open();
    clientSocket.configureBlocking(false);
    boolean connected = clientSocket.connect(
            new InetSocketAddress(InetAddress.getLocalHost(), serverPort));
    assertFalse(connected);
    assertTrue(clientSocket.isConnectionPending());
    eventLoop.registerConnect(clientSocket, serverHandler);
    // registering it for reading as well is not permitted: causes problems on Linux
    try {
        eventLoop.registerRead(clientSocket, serverHandler);
        fail("expected exception");
    } catch (AssertionError e) {}

    // The event loop will trigger the accept callback
    assertNull(serverHandler.client);
    eventLoop.runOnce();
    assertNotNull(serverHandler.client);
    assertTrue(clientSocket.isConnectionPending());

    // The event loop will also have triggered the connect callback
    assertTrue(serverHandler.connected);
    connected = clientSocket.finishConnect();
    assertTrue(connected);
    assertTrue(clientSocket.isConnected());

    // Registering some other handler in response to the connect event should work
    eventLoop.registerRead(clientSocket, new TestHandler());

    clientSocket.close();
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:35,代码来源:NIOEventLoopTest.java

示例4: disconnect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void disconnect(SelectionKey sk){
    SocketChannel sc=(SocketChannel)sk.channel();
    try {
        sc.finishConnect();
    }catch (IOException e){

    }
}
 
开发者ID:sean417,项目名称:LearningOfThinkInJava,代码行数:9,代码来源:MultiThreadNIOEchoServer.java

示例5: connect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
public void connect(SelectionKey key) {
    SocketChannel sc = (SocketChannel) key.channel();
    try {
        while (!sc.finishConnect()) {
            logger.info("connecting to server ....");
        }
        key.interestOps(SelectionKey.OP_READ);
        parser.getWaitFor().add(Protocol.handshake);
        logger.info("connected to server success");
    } catch (IOException e) {
        logger.error("connecting to server error", e);
    }
}
 
开发者ID:xiaoma20082008,项目名称:mysql-protocol,代码行数:15,代码来源:MySQLConnection.java

示例6: connect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
public void connect(SelectionKey key) {
    logger.info("connect to server");
    SocketChannel sc = (SocketChannel) key.channel();
    try {
        while (!sc.finishConnect()) {
        }
        key.interestOps(SelectionKey.OP_READ);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
开发者ID:xiaoma20082008,项目名称:mysql-protocol,代码行数:13,代码来源:ConnectionHandler.java

示例7: openConnection

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static PeerWireConnection openConnection(InetSocketAddress addr) throws IOException {
		SocketChannel channel = SocketChannel.open(addr);
		channel.finishConnect();
//		System.out.println("Are we blocking: " + channel.isBlocking());
//		System.out.println("Are we connected: " + channel.isConnected());

		return new PeerWireConnection(channel, true);
	}
 
开发者ID:jc0541,项目名称:URTorrent,代码行数:9,代码来源:PeerWireConnection.java

示例8: finishConnect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
protected boolean finishConnect(SocketChannel handle) throws Exception {
    if (handle.finishConnect()) {
        SelectionKey key = handle.keyFor(selector);

        if (key != null) {
            key.cancel();
        }

        return true;
    }

    return false;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:18,代码来源:NioSocketConnector.java

示例9: finishConnect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private boolean finishConnect(AbstractConnection c, SocketChannel channel)
		throws IOException {
	if (channel.isConnectionPending()) {
		// 阻塞或非阻塞
		channel.finishConnect();

		c.setLocalPort(channel.socket().getLocalPort());
		return true;
	} else {
		return false;
	}
}
 
开发者ID:huang-up,项目名称:mycat-src-1.6.1-RELEASE,代码行数:13,代码来源:NIOConnector.java

示例10: doTransport

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
void doTransport(int waitTimeOut, List<Packet> pendingQueue, LinkedList<Packet> outgoingQueue,
                 ClientCnxn cnxn)
        throws IOException, InterruptedException {
    selector.select(waitTimeOut);
    Set<SelectionKey> selected;
    synchronized (this) {
        selected = selector.selectedKeys();
    }
    // Everything below and until we get back to the select is
    // non blocking, so time is effectively a constant. That is
    // Why we just have to do this once, here
    updateNow();
    for (SelectionKey k : selected) {
        SocketChannel sc = ((SocketChannel) k.channel());
        if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
            if (sc.finishConnect()) {
                updateLastSendAndHeard();
                sendThread.primeConnection();
            }
        } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {
            doIO(pendingQueue, outgoingQueue, cnxn);
        }
    }
    if (sendThread.getZkState().isConnected()) {
        synchronized(outgoingQueue) {
            if (findSendablePacket(outgoingQueue,
                    cnxn.sendThread.clientTunneledAuthenticationInProgress()) != null) {
                enableWrite();
            }
        }
    }
    selected.clear();
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:35,代码来源:ClientCnxnSocketNIO.java

示例11: connect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void connect(SelectionKey key) throws IOException {
    SocketChannel channel = (SocketChannel) key.channel();
    // 首先判断是否连接已经建立,如果没有,则调用finishConnect()完成连接
    if (channel.isConnectionPending()) {
        channel.finishConnect();

    }
    channel.configureBlocking(false);
    //建立连接后,向Channel写入数据,并同时注册读事件为感兴趣的事件
    channel.write(ByteBuffer.wrap(new String("hello server!\r\n").getBytes()));
    channel.register(this.selector, SelectionKey.OP_READ);
}
 
开发者ID:zhangboqing,项目名称:multithread,代码行数:13,代码来源:NIOEchoClient.java

示例12: finishConnect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private boolean finishConnect(AbstractConnection c, SocketChannel channel)
        throws IOException {
    if (channel.isConnectionPending()) {
        channel.finishConnect();

        c.setLocalPort(channel.socket().getLocalPort());
        return true;
    } else {
        return false;
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:12,代码来源:NIOConnector.java

示例13: finishConnect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private boolean finishConnect(Connection c, SocketChannel channel) throws IOException {
	if (channel.isConnectionPending()) {
		channel.finishConnect();
		c.setLocalPort(channel.socket().getLocalPort());
		return true;
	} else {
		return false;
	}
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:10,代码来源:NIOConnector.java

示例14: doTransport

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
void doTransport(int waitTimeOut, List<Packet> pendingQueue, ClientCnxn cnxn)
        throws IOException, InterruptedException {
    selector.select(waitTimeOut);
    Set<SelectionKey> selected;
    synchronized (this) {
        selected = selector.selectedKeys();
    }
    // Everything below and until we get back to the select is
    // non blocking, so time is effectively a constant. That is
    // Why we just have to do this once, here
    updateNow();
    for (SelectionKey k : selected) {
        SocketChannel sc = ((SocketChannel) k.channel());
        if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
            if (sc.finishConnect()) {
                updateLastSendAndHeard();
                updateSocketAddresses();
                sendThread.primeConnection();
            }
        } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {
            doIO(pendingQueue, cnxn);
        }
    }
    if (sendThread.getZkState().isConnected()) {
        if (findSendablePacket(outgoingQueue,
                sendThread.tunnelAuthInProgress()) != null) {
            enableWrite();
        }
    }
    selected.clear();
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:33,代码来源:ClientCnxnSocketNIO.java

示例15: handleClients

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void handleClients() throws Exception {
    int selectCount = 0;
    while (true) {
        int createdCount = 0;
        synchronized (this) {
            if (connectionsNeeded > 0) {

                while (connectionsNeeded > 0 && createdCount < 20) {
                    connectionsNeeded--;
                    createdCount++;
                    totalCreated++;

                    SocketChannel channel = SocketChannel.open();
                    channel.configureBlocking(false);
                    channel.connect(address);
                    if (!channel.finishConnect()) {
                        channel.register(selector,
                                         SelectionKey.OP_CONNECT);
                    }
                }

                log("Started total of " +
                    totalCreated + " client connections");
                Thread.sleep(200);
            }
        }

        if (createdCount > 0) {
            selector.selectNow();
        } else {
            selectCount++;
            long startTime = System.nanoTime();
            selector.select();
            long duration = durationMillis(startTime);
            log("Exited clientSelector.select(), loop #"
                + selectCount + ", duration = " + duration + "ms");
        }

        int keyCount = -1;
        Iterator<SelectionKey> keys =
            selector.selectedKeys().iterator();
        while (keys.hasNext()) {
            SelectionKey key = keys.next();
            synchronized (key) {
                keyCount++;
                keys.remove();
                if (!key.isValid()) {
                    log("Ignoring client key #" + keyCount);
                    continue;
                }
                int readyOps = key.readyOps();
                if (readyOps == SelectionKey.OP_CONNECT) {
                    key.interestOps(0);
                    ((SocketChannel) key.channel()).finishConnect();
                } else {
                    log("readyOps() on client key #" + keyCount +
                        " returned " + readyOps);
                }
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:63,代码来源:LotsOfCancels.java


注:本文中的java.nio.channels.SocketChannel.finishConnect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。