本文整理了Java中java.nio.channels.SocketChannel.configureBlocking()
方法的一些代码示例,展示了SocketChannel.configureBlocking()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SocketChannel.configureBlocking()
方法的具体详情如下:
包路径:java.nio.channels.SocketChannel
类名称:SocketChannel
方法名:configureBlocking
暂无
代码示例来源:origin: apache/kafka
private void configureSocketChannel(SocketChannel socketChannel, int sendBufferSize, int receiveBufferSize)
throws IOException {
socketChannel.configureBlocking(false);
Socket socket = socketChannel.socket();
socket.setKeepAlive(true);
if (sendBufferSize != Selectable.USE_DEFAULT_BUFFER_SIZE)
socket.setSendBufferSize(sendBufferSize);
if (receiveBufferSize != Selectable.USE_DEFAULT_BUFFER_SIZE)
socket.setReceiveBufferSize(receiveBufferSize);
socket.setTcpNoDelay(true);
}
代码示例来源:origin: apache/zookeeper
/**
* create a socket channel.
* @return the created socket channel
* @throws IOException
*/
SocketChannel createSock() throws IOException {
SocketChannel sock;
sock = SocketChannel.open();
sock.configureBlocking(false);
sock.socket().setSoLinger(false, -1);
sock.socket().setTcpNoDelay(true);
return sock;
}
代码示例来源:origin: apache/nifi
private SocketChannel doConnect(InetSocketAddress addressToConnect) throws IOException {
SocketChannel channel = SocketChannel.open();
if (channel.connect(addressToConnect)) {
channel.configureBlocking(false);
channel.register(this.selector, SelectionKey.OP_READ);
} else {
throw new IllegalStateException("Failed to connect to Server at: " + addressToConnect);
}
return channel;
}
代码示例来源:origin: apache/rocketmq
public static SocketChannel connect(SocketAddress remote, final int timeoutMillis) {
SocketChannel sc = null;
try {
sc = SocketChannel.open();
sc.configureBlocking(true);
sc.socket().setSoLinger(false, -1);
sc.socket().setTcpNoDelay(true);
sc.socket().setReceiveBufferSize(1024 * 64);
sc.socket().setSendBufferSize(1024 * 64);
sc.socket().connect(remote, timeoutMillis);
sc.configureBlocking(false);
return sc;
} catch (Exception e) {
if (sc != null) {
try {
sc.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return null;
}
代码示例来源:origin: rapidoid/rapidoid
private void configureSocket(SocketChannel socketChannel) throws IOException {
socketChannel.configureBlocking(false);
Socket socket = socketChannel.socket();
socket.setTcpNoDelay(noDelay);
socket.setReceiveBufferSize(bufSize);
socket.setSendBufferSize(bufSize);
socket.setReuseAddress(true);
}
代码示例来源:origin: looly/hutool
/**
*
* 简易的使用Socket发送数据
*
* @param host Server主机
* @param port Server端口
* @param isBlock 是否阻塞方式
* @param data 需要发送的数据
* @throws IORuntimeException IO异常
* @since 3.3.0
*/
public static void netCat(String host, int port, boolean isBlock, ByteBuffer data) throws IORuntimeException {
try (SocketChannel channel = SocketChannel.open(createAddress(host, port))) {
channel.configureBlocking(isBlock);
channel.write(data);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
代码示例来源:origin: org.apache.zookeeper/zookeeper
/**
* create a socket channel.
* @return the created socket channel
* @throws IOException
*/
SocketChannel createSock() throws IOException {
SocketChannel sock;
sock = SocketChannel.open();
sock.configureBlocking(false);
sock.socket().setSoLinger(false, -1);
sock.socket().setTcpNoDelay(true);
return sock;
}
代码示例来源:origin: rapidoid/rapidoid
private void configureSocket(SocketChannel socketChannel) throws IOException {
socketChannel.configureBlocking(false);
Socket socket = socketChannel.socket();
socket.setTcpNoDelay(noDelay);
socket.setReceiveBufferSize(bufSize);
socket.setSendBufferSize(bufSize);
socket.setReuseAddress(true);
}
代码示例来源:origin: looly/hutool
/**
*
* 简易的使用Socket发送数据
*
* @param host Server主机
* @param port Server端口
* @param isBlock 是否阻塞方式
* @param data 需要发送的数据
* @throws IORuntimeException IO异常
* @since 3.3.0
*/
public static void netCat(String host, int port, boolean isBlock, ByteBuffer data) throws IORuntimeException {
try (SocketChannel channel = SocketChannel.open(createAddress(host, port))) {
channel.configureBlocking(isBlock);
channel.write(data);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
代码示例来源:origin: alibaba/cobar
protected SocketChannel openSocketChannel() throws IOException {
SocketChannel channel = null;
try {
channel = SocketChannel.open();
channel.configureBlocking(false);
Socket socket = channel.socket();
socket.setReceiveBufferSize(socketRecvBuffer);
socket.setSendBufferSize(socketSendBuffer);
socket.setTcpNoDelay(true);
socket.setKeepAlive(true);
} catch (IOException e) {
closeChannel(channel);
throw e;
} catch (RuntimeException e) {
closeChannel(channel);
throw e;
}
return channel;
}
代码示例来源:origin: apache/rocketmq
public HAConnection(final HAService haService, final SocketChannel socketChannel) throws IOException {
this.haService = haService;
this.socketChannel = socketChannel;
this.clientAddr = this.socketChannel.socket().getRemoteSocketAddress().toString();
this.socketChannel.configureBlocking(false);
this.socketChannel.socket().setSoLinger(false, -1);
this.socketChannel.socket().setTcpNoDelay(true);
this.socketChannel.socket().setReceiveBufferSize(1024 * 64);
this.socketChannel.socket().setSendBufferSize(1024 * 64);
this.writeSocketService = new WriteSocketService(this.socketChannel);
this.readSocketService = new ReadSocketService(this.socketChannel);
this.haService.getConnectionCount().incrementAndGet();
}
代码示例来源:origin: mpetazzoni/ttorrent
private void connectToPeersFromQueue() {
ConnectTask connectTask;
while ((connectTask = myConnectQueue.poll()) != null) {
if (stop || Thread.currentThread().isInterrupted()) {
return;
}
logger.debug("try connect to peer. Connect task is {}", connectTask);
try {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_CONNECT, connectTask);
socketChannel.connect(new InetSocketAddress(connectTask.getHost(), connectTask.getPort()));
} catch (IOException e) {
LoggerUtils.warnAndDebugDetails(logger, "unable connect. Connect task is {}", connectTask, e);
}
}
}
代码示例来源:origin: apache/nifi
public SSLCommsSession(final SSLContext sslContext, final String hostname, final int port, final int timeoutMillis) throws IOException {
final SocketChannel socketChannel = SocketChannel.open();
socketChannel.socket().connect(new InetSocketAddress(hostname, port), timeoutMillis);
socketChannel.configureBlocking(false);
sslSocketChannel = new SSLSocketChannel(sslContext, socketChannel,true);
in = new SSLSocketChannelInputStream(sslSocketChannel);
bufferedIn = new BufferedInputStream(in);
out = new SSLSocketChannelOutputStream(sslSocketChannel);
bufferedOut = new BufferedOutputStream(out);
this.sslContext = sslContext;
this.hostname = hostname;
this.port = port;
}
代码示例来源:origin: apache/incubator-gobblin
ReadWriteHandler(SocketChannel proxy, ByteBuffer mixedServerResponseBuffer, SocketChannel client, Selector selector)
throws IOException {
this.proxy = proxy;
this.client = client;
this.selector = selector;
// drain response that is not part of proxy's 200 OK and is part of data pushed from server, and push to client
if (mixedServerResponseBuffer.limit() > mixedServerResponseBuffer.position()) {
this.client.configureBlocking(true);
OutputStream clientOut = this.client.socket().getOutputStream();
clientOut.write(mixedServerResponseBuffer.array(), mixedServerResponseBuffer.position(),
mixedServerResponseBuffer.limit() - mixedServerResponseBuffer.position());
clientOut.flush();
}
this.proxy.configureBlocking(false);
this.client.configureBlocking(false);
this.client.register(this.selector, OP_READ, this);
this.proxy.register(this.selector, OP_READ, this);
}
代码示例来源:origin: wildfly/wildfly
public void run() {
try {
ch=SocketChannel.open();
ch.configureBlocking(true); // we want blocking behavior
ch.connect(new InetSocketAddress(host, 7500));
latch.await();
}
catch(Exception e) {
e.printStackTrace();
}
for(;;) {
total_bytes_sent.add(NioServerPerfTest.SIZE);
if(total_bytes_sent.sum() > NioServerPerfTest.BYTES_TO_SEND)
break;
buf.rewind();
try {
ch.write(buf);
total_msgs.increment();
}
catch(IOException e) {
e.printStackTrace();
}
}
Util.close(ch);
}
}
代码示例来源:origin: apache/nifi
public StandardCommsSession(final String hostname, final int port, final int timeoutMillis) throws IOException {
socketChannel = SocketChannel.open();
socketChannel.socket().connect(new InetSocketAddress(hostname, port), timeoutMillis);
socketChannel.configureBlocking(false);
in = new SocketChannelInputStream(socketChannel);
bufferedIn = new InterruptableInputStream(new BufferedInputStream(in));
out = new SocketChannelOutputStream(socketChannel);
bufferedOut = new InterruptableOutputStream(new BufferedOutputStream(out));
this.hostname = hostname;
this.port = port;
}
代码示例来源:origin: jMonkeyEngine/jmonkeyengine
protected void accept( SelectionKey key ) throws IOException
{
// Would only get accepts on a server channel
ServerSocketChannel serverChan = (ServerSocketChannel)key.channel();
// Setup the connection to be non-blocking
SocketChannel remoteChan = serverChan.accept();
remoteChan.configureBlocking(false);
// And disable Nagle's buffering algorithm... we want
// data to go when we put it there.
Socket sock = remoteChan.socket();
sock.setTcpNoDelay(true);
// Let the selector know we're interested in reading
// data from the channel
SelectionKey endKey = remoteChan.register( selector, SelectionKey.OP_READ );
// And now create a new endpoint
NioEndpoint p = addEndpoint( remoteChan );
endKey.attach(p);
endpointKeys.put(p, endKey);
}
代码示例来源:origin: apache/incubator-gobblin
ProxySetupHandler(SocketChannel client, Selector selector, Config config) throws IOException {
this.config = config;
this.client = client;
this.selector = selector;
this.buffer =
ByteBuffer
.wrap(String
.format(
"CONNECT %s:%d HTTP/1.1\r%nUser-Agent: GobblinTunnel\r%nservice-name: gobblin\r%n"
+ "Connection: keep-alive\r%nHost: %s:%d\r%n\r%n",
config.getRemoteHost(), config.getRemotePort(), config.getRemoteHost(), config.getRemotePort())
.getBytes(ConfigurationKeys.DEFAULT_CHARSET_ENCODING));
//Blocking call
this.proxy = SocketChannel.open();
this.proxy.configureBlocking(false);
this.connectStartTime = System.currentTimeMillis();
boolean connected =
this.proxy.connect(new InetSocketAddress(this.config.getProxyHost(), this.config.getProxyPort()));
if (!connected) {
this.client.configureBlocking(false);
this.client.register(this.selector, SelectionKey.OP_READ, this);
this.proxy.register(this.selector, SelectionKey.OP_CONNECT, this);
} else {
this.state = HandlerState.WRITING;
this.proxy.register(this.selector, SelectionKey.OP_WRITE, this);
}
}
代码示例来源:origin: apache/nifi
private SocketChannel createChannel() throws IOException {
final SocketChannel socketChannel = SocketChannel.open();
try {
socketChannel.configureBlocking(true);
final Socket socket = socketChannel.socket();
socket.setSoTimeout(timeoutMillis);
socket.connect(new InetSocketAddress(nodeIdentifier.getLoadBalanceAddress(), nodeIdentifier.getLoadBalancePort()));
socket.setSoTimeout(timeoutMillis);
return socketChannel;
} catch (final Exception e) {
try {
socketChannel.close();
} catch (final Exception closeException) {
e.addSuppressed(closeException);
}
throw e;
}
}
代码示例来源:origin: EsotericSoftware/kryonet
public SelectionKey accept (Selector selector, SocketChannel socketChannel) throws IOException {
writeBuffer.clear();
readBuffer.clear();
readBuffer.flip();
currentObjectLength = 0;
try {
this.socketChannel = socketChannel;
socketChannel.configureBlocking(false);
Socket socket = socketChannel.socket();
socket.setTcpNoDelay(true);
selectionKey = socketChannel.register(selector, SelectionKey.OP_READ);
if (DEBUG) {
debug("kryonet", "Port " + socketChannel.socket().getLocalPort() + "/TCP connected to: "
+ socketChannel.socket().getRemoteSocketAddress());
}
lastReadTime = lastWriteTime = System.currentTimeMillis();
return selectionKey;
} catch (IOException ex) {
close();
throw ex;
}
}
内容来源于网络,如有侵权,请联系作者删除!