當前位置: 首頁>>代碼示例>>Java>>正文


Java AsynchronousSocketChannel.setOption方法代碼示例

本文整理匯總了Java中java.nio.channels.AsynchronousSocketChannel.setOption方法的典型用法代碼示例。如果您正苦於以下問題:Java AsynchronousSocketChannel.setOption方法的具體用法?Java AsynchronousSocketChannel.setOption怎麽用?Java AsynchronousSocketChannel.setOption使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.nio.channels.AsynchronousSocketChannel的用法示例。


在下文中一共展示了AsynchronousSocketChannel.setOption方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: handleNewConnection

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
/**
   * Creates a new client and adds it to the list of connections.
   * Sets the clients handler to the initial state of NameReader
   *
   * @param channel the newly accepted channel
   */
private void handleNewConnection(AsynchronousSocketChannel channel) {
	try {
		channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
	} catch (IOException e) {
		// ignore
		//
		e.printStackTrace();
	}

	//new dealer and channel
	AioServerDataDealer dealer = null;
	
	dealer = aioDataDealerFactory.getAioServerDataDealer();
	
	int channelId = getChannelId();
	AioServerChannel aioChannel = new AioServerChannel(channelId, channel, dealer, this);
	connections.put(channelId, aioChannel);
	
	//start channel
	aioChannel.run(null);
}
 
開發者ID:psfu,項目名稱:waterwave,代碼行數:28,代碼來源:AioServer.java

示例2: retry

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
private void retry() {
    try {
        TimeUnit.SECONDS.sleep(1);
        if (null != this.channel && this.channel.isOpen()) {
            this.channel.close();
        }
        log.debug("連接:{}", this.socketAddress.toString());
        final AsynchronousSocketChannel asynchronousSocketChannel = AsynchronousSocketChannel.open(this.group);
        asynchronousSocketChannel.setOption(StandardSocketOptions.TCP_NODELAY, true);
        asynchronousSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        asynchronousSocketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
        asynchronousSocketChannel.connect(this.socketAddress).get(5, TimeUnit.SECONDS);
        this.channel = new FastChannel(asynchronousSocketChannel, this.serializer, timeout);
    } catch (final Exception e) {
        retry();
    }
}
 
開發者ID:sd4324530,項目名稱:fastrpc,代碼行數:18,代碼來源:FastRpcClient.java

示例3: testSingleConnect

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
public void testSingleConnect() throws IOException, ExecutionException, InterruptedException, TimeoutException {
	AsynchronousSocketChannel serverConnectionChannel = null;
	try {
		assertNotNull(serverAddress);

		serverConnectionChannel = AsynchronousSocketChannel.open();
		serverConnectionChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
		
		// Ensure we have have returned an open connection
		assertNotNull(serverConnectionChannel);
		assertTrue("Channel is not open", serverConnectionChannel.isOpen());

		// Blocking connect
		Future<Void> future = serverConnectionChannel.connect(serverAddress);
		future.get(getTimeout(), TimeUnit.MILLISECONDS);
		
		// Ensure we are connected
		assertNotNull("Unable to get remote address", serverConnectionChannel.getRemoteAddress());
		
	} finally {
		if (serverConnectionChannel != null) {
			try {
				serverConnectionChannel.close();
			} catch (ClosedChannelException cce) {
				// That's ok
			}
			assertFalse("Channel was not closed", serverConnectionChannel.isOpen());
		}
	}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-systemtest,代碼行數:31,代碼來源:MultipleConnectFutureTest.java

示例4: openSocketChannel

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
protected AsynchronousSocketChannel openSocketChannel(
        AsynchronousChannelGroup asynchronousChannelGroup) throws IOException {
    AsynchronousSocketChannel channel =
            AsynchronousSocketChannel.open(asynchronousChannelGroup);
    channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, false);
    channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
    return channel;
}
 
開發者ID:flownclouds,項目名稱:Timo,代碼行數:10,代碼來源:BackendConnectionFactory.java

示例5: openSocketChannel

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
protected AsynchronousSocketChannel openSocketChannel() throws IOException {
	AsynchronousSocketChannel channel = AsynchronousSocketChannel
			.open(MycatServer.getInstance().getNextAsyncChannelGroup());
	channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
	channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);

	return channel;
}
 
開發者ID:youngor,項目名稱:openclouddb,代碼行數:10,代碼來源:BackendConnectionFactory.java

示例6: make

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
public FrontendConnection make(AsynchronousSocketChannel channel)
		throws IOException {
	channel.setOption(StandardSocketOptions.SO_RCVBUF, socketRecvBuffer);
	channel.setOption(StandardSocketOptions.SO_SNDBUF, socketSendBuffer);
	channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
	channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);

	FrontendConnection c = getConnection(channel);
	c.setPacketHeaderSize(packetHeaderSize);
	c.setMaxPacketSize(maxPacketSize);
	c.setWriteQueue(new BufferQueue(writeQueueCapcity));
	c.setIdleTimeout(idleTimeout);
	c.setCharset(charset);
	return c;
}
 
開發者ID:youngor,項目名稱:openclouddb,代碼行數:16,代碼來源:FrontendConnectionFactory.java

示例7: onChannelCreated

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
@Override
public int onChannelCreated(AsynchronousSocketChannel channel, Object attachment) throws IOException
{
	super.onChannelCreated(channel, attachment);
	channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
	return 0;
}
 
開發者ID:dwing4g,項目名稱:jane,代碼行數:8,代碼來源:TestEchoAio.java

示例8: onChannelCreated

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
/**
 * 連接創建且在TcpSession創建前響應一次. 可以修改一些連接的設置
 * @param channel
 * @param attachment 作為客戶端建立的連接時為startClient傳入的參數; 作為服務器建立的連接時為null
 * @return 返回>=0表示讀緩衝區大小(每次最多讀的字節數,0表示取默認值);返回<0表示斷開連接,不再創建TcpSession
 */
@SuppressWarnings("static-method")
public int onChannelCreated(AsynchronousSocketChannel channel, Object attachment) throws IOException
{
	channel.setOption(StandardSocketOptions.TCP_NODELAY, false);
	channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	channel.setOption(StandardSocketOptions.SO_KEEPALIVE, false);
	channel.setOption(StandardSocketOptions.SO_RCVBUF, TcpSession.DEF_RECV_SOBUF_SIZE);
	channel.setOption(StandardSocketOptions.SO_SNDBUF, TcpSession.DEF_SEND_SOBUF_SIZE);
	return 0;
}
 
開發者ID:dwing4g,項目名稱:jane,代碼行數:17,代碼來源:TcpManager.java

示例9: testMultipleConnectSequential

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
public void testMultipleConnectSequential() throws IOException, ExecutionException, InterruptedException, TimeoutException {
	assertNotNull(serverAddress);
	System.out.println("MultipleConnectFutureTest.testMultipleConnectSequential() creating " +
			numberOfConnections + " connections");
	for (int counter = 0; counter < numberOfConnections; counter++) {
		HangNotifier.ping();
		AsynchronousSocketChannel serverConnectionChannel = null;
		try {
			serverConnectionChannel = AsynchronousSocketChannel.open();
			serverConnectionChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
			
			// Ensure we have have returned an open connection
			assertNotNull(serverConnectionChannel);
			assertTrue("Channel is not open on iteration " + counter, serverConnectionChannel.isOpen());
	
			// Blocking connect
			Future<Void> future = serverConnectionChannel.connect(serverAddress);
			future.get(getTimeout(), TimeUnit.MILLISECONDS);
			
			// Ensure we are connected
			assertNotNull("Unable to get remote address on iteration " + counter, serverConnectionChannel.getRemoteAddress());
		} finally {
			if (serverConnectionChannel != null) {
				try {
					serverConnectionChannel.close();
				} catch (ClosedChannelException cce) {
					// That's ok
				} catch (IOException ioe) {
					throw ioe;
				}
				assertFalse("Channel was not closed on iteration " + counter, serverConnectionChannel.isOpen());
			}
		}
		
		// We can't hammer the server continuously, or we'll run out of resource.
		// Sleep before the test claims completion
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {}
	}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-systemtest,代碼行數:42,代碼來源:MultipleConnectFutureTest.java

示例10: createListener

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
/**
 * 
 * 
 * @param channelGroup
 * @return
 * @throws IOException
 * 
 * 
 *             SO_SNDBUF The size of the socket send buffer .
 *             SO_RCVBUF The size of the socket receive buffer. 
 *             SO_KEEPALIVE Keep connection alive. 
 *             SO_REUSEADDR Re-use address .
 *             TCP_NODELAY Disable the Nagle algorithm.
 * 
 * 
 */
private AsynchronousSocketChannel createListener(AsynchronousChannelGroup channelGroup) throws IOException {
	final AsynchronousSocketChannel listener = AsynchronousSocketChannel.open(channelGroup);
	//TODO
	//listener.setOption(StandardSocketOptions.TCP_NODELAY, true);
	listener.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	listener.setOption(StandardSocketOptions.SO_RCVBUF, 16 * 1024);
	listener.setOption(StandardSocketOptions.SO_SNDBUF, 16 * 1024);
	return listener;
}
 
開發者ID:psfu,項目名稱:waterwave,代碼行數:26,代碼來源:AioClient.java


注:本文中的java.nio.channels.AsynchronousSocketChannel.setOption方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。