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


Java SocketChannel.open方法代码示例

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


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

示例1: 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,项目名称:sstore-soft,代码行数:21,代码来源:TestHStoreCoordinator.java

示例2: open

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
 * Open client socket and connect to given address and port.
 *
 * @param address Address to connect to.
 * @param port Port to connect to.
 * @return {@link TCPClientSocket}
 * @throws IOException If an I/O error occurs.
 */
static TCPClientSocketImpl open(String address, int port) throws IOException {

    TCPClientSocketImpl clientSocket = null;
    try {
        clientSocket = new TCPClientSocketImpl(SocketChannel.open());
        clientSocket.socketChannel.socket().setSoTimeout(DEFAULT_SOCKET_TIMEOUT_IN_MS);
        clientSocket.socketChannel.connect(new InetSocketAddress(address, port));
        clientSocket.socketChannel.configureBlocking(false);
    } catch(Exception e) {
        if (clientSocket != null) {
            clientSocket.close();
        }
        throw e;
    }

    return clientSocket;
}
 
开发者ID:cpppwner,项目名称:NoRiskNoFun,代码行数:26,代码来源:TCPClientSocketImpl.java

示例3: testClientReconnect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Test
public void testClientReconnect() throws IOException, InterruptedException {
    HostProvider hostProvider = mock(HostProvider.class);
    when(hostProvider.size()).thenReturn(1);
    InetSocketAddress inaddr = new InetSocketAddress(1111);
    when(hostProvider.next(anyLong())).thenReturn(inaddr);
    ZooKeeper zk = mock(ZooKeeper.class);
    sc =  SocketChannel.open();

    ClientCnxnSocketNIO nioCnxn = new MockCnxn();
    ClientWatchManager watcher = mock(ClientWatchManager.class);
    ClientCnxn clientCnxn = new ClientCnxn(
            "tmp", hostProvider, 5000,
            zk, watcher, nioCnxn, false);
    clientCnxn.start();
    countDownLatch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(countDownLatch.getCount() == 0);
    clientCnxn.close();
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:20,代码来源:ClientReconnectTest.java

示例4: main

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void main(String[] argv) throws Exception {
    try (ByteServer server = new ByteServer();
         SocketChannel sc = SocketChannel.open(server.address())) {

        server.acceptConnection();

        try (Selector sel = Selector.open()) {
            sc.configureBlocking(false);
            sc.register(sel, SelectionKey.OP_WRITE);
            sel.select();
            sel.selectedKeys().clear();
            if (sel.select() == 0) {
                throw new Exception("Select returned zero");
            }
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:SelectWrite.java

示例5: run

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void run(Consumer<HttpResponse> consumer) throws IOException {
System.out.println("Expecting a response after GET from: " + this.url.toString() + "\n\n");
try (SocketChannel socketChannel = SocketChannel.open(this.address)) {
    socketChannel.configureBlocking(true);
    writeGetRequestTo(socketChannel);
    HttpResponseReader.fromChannel(socketChannel).findFirst().ifPresent(consumer);

}
System.out.println("Demo end");
   }
 
开发者ID:Oliver-Loeffler,项目名称:NIOHttp,代码行数:11,代码来源:StreamDemo.java

示例6: 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

示例7: FDTNetPerfClient

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
FDTNetPerfClient(String host, int port) throws Exception {
    this();
    this.sc = SocketChannel.open();
    this.sc.configureBlocking(true);
    this.sc.socket().connect(new InetSocketAddress(InetAddress.getByName(host), port));
    shouldWrite = true;
}
 
开发者ID:fast-data-transfer,项目名称:fdt,代码行数:8,代码来源:FDTNetPerf.java

示例8: Tunnel

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public Tunnel(InetSocketAddress serverAddress,Selector selector) throws IOException{
	SocketChannel innerChannel=SocketChannel.open();
	innerChannel.configureBlocking(false);
	this.m_InnerChannel=innerChannel;
	this.m_Selector=selector;
	this.m_ServerEP=serverAddress;
	SessionCount++;
}
 
开发者ID:w22ee,项目名称:onekey-proxy-android,代码行数:9,代码来源:Tunnel.java

示例9: get

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
public SocketChannel get(int retryNumber, Throwable lastError) {
    if(lastError != null) {
        logger.error(lastError.getMessage(), lastError);
    }
    try {
        return SocketChannel.open(new InetSocketAddress(host, port));
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
开发者ID:saladinkzn,项目名称:spring-data-tarantool,代码行数:12,代码来源:SimpleSocketChannelProvider.java

示例10: main

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void main(String[] argv) throws Exception {
    try (ByteServer server = new ByteServer();
         SocketChannel sc = SocketChannel.open(server.address())) {

        server.acceptConnection();

        try (Selector sel = Selector.open()) {
            sc.configureBlocking(false);
            sc.register(sel, SelectionKey.OP_READ);
            // Previously channel would get selected here, although there is nothing to read
            if (sel.selectNow() != 0)
                throw new Exception("Select returned nonzero value");
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:16,代码来源:ReadAfterConnect.java

示例11: init

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void init() throws Exception {

        sel = Selector.open();

        InetSocketAddress addr = new InetSocketAddress(serverHost, serverPort);
        for (int i = 0; i < sockNum; i++) {
            SocketChannel sc = SocketChannel.open();

            sc.configureBlocking(false);

            System.out.println("initiating connection");

            sc.connect(addr);

            // TODO ... for the moment there is a 1-1 mapping between "filling threads" and number of sockets ...
            Thread t = new Thread(new FillingTask());
            t.setDaemon(true);
            t.start();

            while (!sc.finishConnect()) {
                // TODO - do something useful
                try {
                    Thread.sleep(100);
                } catch (Exception ex) {
                }
                ;
            }

            System.out.println("connection established");
            sc.register(sel, SelectionKey.OP_WRITE);
        }

    }
 
开发者ID:fast-data-transfer,项目名称:fdt,代码行数:34,代码来源:JIperfClient.java

示例12: SocketChannelHelper

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public SocketChannelHelper(String address, int port)
 	{
 		this.address = address;
 		this.port = port;

 		try
 		{
	this.channel = SocketChannel.open(new InetSocketAddress(address, port));
}
 		catch (IOException e)
 		{
	e.printStackTrace();
}
 	}
 
开发者ID:Yarichi,项目名称:Proyecto-DASI,代码行数:15,代码来源:TCPSocketHelper.java

示例13: main

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    InetAddress lh = InetAddress.getLocalHost();
    InetSocketAddress isa = new InetSocketAddress(lh, 12345);

    System.setSecurityManager( new SecurityManager() );
    for (int i=0; i<100000; i++) {
        try {
            SocketChannel.open(isa);
            throw new RuntimeException("This should not happen");
        } catch (SecurityException x) { }
    }

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:OpenLeak.java

示例14: 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

示例15: main

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(0));

    InetAddress lh = InetAddress.getLocalHost();
    final SocketChannel sc = SocketChannel.open();
    final InetSocketAddress isa =
        new InetSocketAddress(lh, ssc.socket().getLocalPort());

    // establish connection in another thread
    Runnable connector =
        new Runnable() {
            public void run() {
                try {
                    sc.connect(isa);
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        };
    Thread thr = new Thread(connector);
    thr.start();

    // wait for connect to be established and for thread to
    // terminate
    do {
        try {
            thr.join();
        } catch (InterruptedException x) { }
    } while (thr.isAlive());

    // check connection is established
    if (!sc.isConnected()) {
        throw new RuntimeException("SocketChannel not connected");
    }

    // close channel - this triggered the bug as it attempted to signal
    // a thread that no longer exists
    sc.close();

    // clean-up
    ssc.accept().close();
    ssc.close();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:45,代码来源:CloseAfterConnect.java


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