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


Java SocketChannel.connect方法代码示例

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


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

示例1: run

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void run() throws Exception
{
	NioThread nt=new NioThread();
	nt.start();
	Thread.sleep(1000);
	SocketChannel sc=SocketChannel.open();
	sc.configureBlocking(false);
	sc.connect(new InetSocketAddress("localhost", 9999));
	ChannelProcessorMultiplexer m=new ChannelProcessorMultiplexer(nt, sc, true,
			VideoServerTCPListener.clientID.getBytes(StandardCharsets.UTF_8),
			VideoServerTCPListener.serverID.getBytes(StandardCharsets.UTF_8));
	DuplexNioConnection conn=new DuplexNioConnection(m);
	m.start();
	CoolRMIClient client=new CoolRMIClient(getClass().getClassLoader(), new RMINioClientConnectionFactory(conn), false);
	Iremote r= (Iremote)client.getService(Iremote.class, Iremote.class.getName());
	System.out.println(""+r.getValue("Kitten"));
	System.out.println(""+r.getValue("Kitten"));
	System.out.println(""+r.getValue("Kitten"));
	System.out.println(""+r.getValue("Kitten"));
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:21,代码来源:RMINioClient.java

示例2: openConnection

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
 * out node try to connect
 *
 * @param node
 */
public void openConnection(Node node) {

    SocketChannel channel = null;
    try {
        channel = SocketChannel.open();
        channel.configureBlocking(false);
        channel.socket().setReuseAddress(true);
        InetSocketAddress socketAddress = new InetSocketAddress(node.getIp(), node.getPort());
        channel.connect(socketAddress);
        PendingConnect data = new PendingConnect(channel, node);
        SelectionKey key = channel.register(selector, SelectionKey.OP_CONNECT);
        key.attach(data);
        selector.wakeup();
    } catch (IOException e) {
        e.printStackTrace();
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        node.destroy();
    }
}
 
开发者ID:nuls-io,项目名称:nuls,代码行数:31,代码来源:ConnectionManager.java

示例3: testStartConnection

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
 * testStartConnection
 */
@Test
public void testStartConnection() throws Exception {
    System.err.println("testStartConnection()");
    for (final HStoreCoordinator m : this.coordinators) {
        // Check that the messenger state is correct
        assert (m.isStarted());

        // Check that the messenger's listener thread is running
        assert (m.getListenerThread().isAlive());

        // Check that we can connect to the messenger's listening port
        int port = m.getLocalMessengerPort();
        SocketChannel channel = SocketChannel.open();
        channel.connect(new InetSocketAddress(port));
        assert (channel.isConnected());
    } // FOR
}
 
开发者ID:s-store,项目名称:s-store,代码行数:21,代码来源:TestHStoreCoordinator.java

示例4: connect2

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void connect2(Socket socket, 
          SocketAddress endpoint, 
          int timeout) throws IOException {
	  SocketChannel socketChannel = socket.getChannel();
		// ����Ϊ��������ʽ
		socketChannel.configureBlocking(false);
		// ��ѡ����
		Selector selector = Selector.open();
		// ע�����ӷ����socket����
		socketChannel.register(selector, SelectionKey.OP_CONNECT);
		// ����
		socketChannel.connect(endpoint);	 




}
 
开发者ID:spafka,项目名称:spark_deep,代码行数:18,代码来源:NetUtils.java

示例5: run

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void run() throws Exception
{
	NioThread nt=new NioThread();
	nt.start();
	Thread.sleep(1000);
	SocketChannel sc=SocketChannel.open();
	sc.configureBlocking(false);
	sc.connect(new InetSocketAddress("localhost", 9999));
	ChannelProcessorMultiplexer m=new ChannelProcessorMultiplexer(nt, sc, true,
			VideoServerTCPListener.clientID.getBytes(StandardCharsets.UTF_8),
			VideoServerTCPListener.serverID.getBytes(StandardCharsets.UTF_8));
	m.start();
	new ConnectToStdout(m);
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:15,代码来源:ExampleNioStdoutClient.java

示例6: registerAndConnect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
 * register with the selection and connect
 * @param sock the {@link SocketChannel} 
 * @param addr the address of remote host
 * @throws IOException
 */
void registerAndConnect(SocketChannel sock, InetSocketAddress addr) 
throws IOException {
    sockKey = sock.register(selector, SelectionKey.OP_CONNECT);
    boolean immediateConnect = sock.connect(addr);
    if (immediateConnect) {
        sendThread.primeConnection();
    }
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:15,代码来源:ClientCnxnSocketNIO.java

示例7: createChannel

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private SocketChannel createChannel() throws IOException {
    logi(TAG, "Open");
    SocketChannel socketChannel = SocketChannel.open();
    socketChannel.configureBlocking(false);
    socketChannel.connect(getRewrittenDestination());
    return socketChannel;
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:8,代码来源:TCPConnection.java

示例8: initClient

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void initClient(String ip, int port) throws IOException { // 获得一个Socket通道
    SocketChannel channel = SocketChannel.open(); // 设置通道为非阻塞
    channel.configureBlocking(false); // 获得一个通道管理器
    this.selector = Selector.open(); // 客户端连接服务器,其实方法执行并没有实现连接,需要在listen()方法中调
    channel.connect(new InetSocketAddress(ip, port));
    channel.register(selector, SelectionKey.OP_CONNECT);
}
 
开发者ID:laidu,项目名称:java-learn,代码行数:8,代码来源:NioClient.java

示例9: connect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void connect(NioThread nt, SocketAddress address, byte[] thisId, byte[] otherId) throws Exception
{
	SocketChannel sc=SocketChannel.open();
	sc.configureBlocking(false);
	sc.connect(address);
	connect(nt, sc, true, thisId, otherId);
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:8,代码来源:CoolRMINioClient.java

示例10: 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,项目名称:s-store,代码行数:35,代码来源:NIOEventLoopTest.java

示例11: connect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void connect(Selector selector) {
	Connection c = null;
	while ((c = connectQueue.poll()) != null) {
		try {
			SocketChannel channel = (SocketChannel) c.getChannel();
			//注册OP_CONNECT监听与后端连接是否真正建立
			channel.register(selector, SelectionKey.OP_CONNECT, c);
			 //主动连接
			channel.connect(new InetSocketAddress(c.host, c.port));
		} catch (Exception e) {
			LOGGER.error("error:", e);
			c.close("connect failed:" + e.toString());
		}
	}
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:16,代码来源:NIOConnector.java

示例12: run

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void run() throws Exception
{
	NioThread nt=new NioThread();
	nt.start();
	Thread.sleep(1000);
	SocketChannel sc=SocketChannel.open();
	sc.configureBlocking(false);
	sc.connect(new InetSocketAddress("localhost", 9999));
	ChannelProcessorMultiplexer m=new ChannelProcessorMultiplexer(nt, sc, true,
			VideoServerTCPListener.clientID.getBytes(StandardCharsets.UTF_8),
			VideoServerTCPListener.serverID.getBytes(StandardCharsets.UTF_8));
	m.start();
	MainChannel mc=new MainChannel();
	mc.register(m);
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:16,代码来源:ExampleNioClient.java

示例13: connect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void connect(Selector finalSelector) {
    AbstractConnection c;
    while ((c = connectQueue.poll()) != null) {
        try {
            SocketChannel channel = (SocketChannel) c.getChannel();
            channel.register(finalSelector, SelectionKey.OP_CONNECT, c);
            channel.connect(new InetSocketAddress(c.host, c.port));

        } catch (Exception e) {
            LOGGER.info("error:", e);
            c.close(e.toString());
        }
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:15,代码来源:NIOConnector.java

示例14: 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:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:63,代码来源:LotsOfCancels.java

示例15: connect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
 * Connect a Session to a server
 * 
 * @param session
 * @throws IOException
 */
void connect(Session session) throws IOException
{
	SocketChannel sChannel = SocketChannel.open();

	sChannel.configureBlocking(false);

	sChannel.connect(new InetSocketAddress(session.getRequestedConnection().getHostName(), session.getRequestedConnection().getPort()));

	sChannel.register(selector, sChannel.validOps());

	Connection con = new Connection(this, sChannel, session);
	session.setConnection(con);

	socChanMap.put(sChannel, session);
}
 
开发者ID:Esri,项目名称:defense-solutions-proofs-of-concept,代码行数:22,代码来源:ConnectionManager.java


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