當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。