本文整理了Java中java.nio.channels.SocketChannel.open()
方法的一些代码示例,展示了SocketChannel.open()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SocketChannel.open()
方法的具体详情如下:
包路径:java.nio.channels.SocketChannel
类名称:SocketChannel
方法名:open
[英]Creates an open and unconnected socket channel.
This channel is created by calling openSocketChannel() of the default SelectorProvider instance.
[中]创建一个开放且未连接的套接字通道。
此通道是通过调用默认SelectorProvider实例的OpenSocketCannel()创建的。
代码示例来源:origin: prestodb/presto
@Override
public Socket createSocket()
throws IOException
{
return SocketChannel.open().socket();
}
代码示例来源:origin: prestodb/presto
@Override
public Socket createSocket(String host, int port)
throws IOException
{
return SocketChannel.open(new InetSocketAddress(host, port)).socket();
}
代码示例来源: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/activemq
@Override
public Socket createSocket(InetAddress address, int port) throws IOException {
SocketChannel channel = SocketChannel.open();
channel.connect(new InetSocketAddress(address, port));
return channel.socket();
}
代码示例来源:origin: wildfly/wildfly
public void start(String ... options) throws Exception {
options(options);
if(server) { // simple single threaded server, can only handle a single connection at a time
srv_channel=ServerSocketChannel.open();
srv_channel.bind(new InetSocketAddress(host, port), 50);
System.out.println("server started (ctrl-c to kill)");
for(;;) {
client_channel=srv_channel.accept();
// client_channel.socket().setTcpNoDelay(true); // we're concerned about latency
receiver_thread=new Receiver();
receiver_thread.start();
}
}
else {
client_channel=SocketChannel.open();
//client_channel.socket().setTcpNoDelay(true);
client_channel.connect(new InetSocketAddress(host, port));
receiver_thread=new Receiver();
receiver_thread.start();
}
}
代码示例来源:origin: apache/nifi
public SSLSocketChannel(final SSLContext sslContext, final String hostname, final int port, final InetAddress localAddress, final boolean client) throws IOException {
this.socketAddress = new InetSocketAddress(hostname, port);
this.channel = SocketChannel.open();
if (localAddress != null) {
final SocketAddress localSocketAddress = new InetSocketAddress(localAddress, 0);
this.channel.bind(localSocketAddress);
}
this.hostname = hostname;
this.port = port;
this.engine = sslContext.createSSLEngine();
this.engine.setUseClientMode(client);
engine.setNeedClientAuth(true);
streamInManager = new BufferStateManager(ByteBuffer.allocate(engine.getSession().getPacketBufferSize()));
streamOutManager = new BufferStateManager(ByteBuffer.allocate(engine.getSession().getPacketBufferSize()));
appDataManager = new BufferStateManager(ByteBuffer.allocate(engine.getSession().getApplicationBufferSize()));
}
代码示例来源:origin: AsyncHttpClient/async-http-client
remote = SocketChannel.open(new InetSocketAddress(remoteAddr, port));
throw new IOException("connect failed");
remote.configureBlocking(false);
remote.register(selector, SelectionKey.OP_READ);
代码示例来源: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: 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: h2oai/h2o-2
public SocketChannel getTCPSocket() throws IOException {
// Under lock, claim an existing open socket if possible
synchronized(this) {
// Limit myself to the number of open sockets from node-to-node
while( _socksAvail == 0 )
try { wait(); } catch( InterruptedException ie ) { }
// Claim an open socket
SocketChannel sock = _socks[--_socksAvail];
if( sock != null ) {
if( sock.isOpen() ) return sock; // Return existing socket!
// Else its an already-closed socket, lower open TCP count
assert TCPS.get() > 0;
TCPS.decrementAndGet();
}
}
// Must make a fresh socket
SocketChannel sock2 = SocketChannel.open();
sock2.socket().setReuseAddress(true);
sock2.socket().setSendBufferSize(AutoBuffer.BBSIZE);
boolean res = sock2.connect( _key );
assert res && !sock2.isConnectionPending() && sock2.isBlocking() && sock2.isConnected() && sock2.isOpen();
TCPS.incrementAndGet(); // Cluster-wide counting
return sock2;
}
public synchronized void freeTCPSocket( SocketChannel sock ) {
代码示例来源:origin: robovm/robovm
/**
* Creates a socket channel and connects it to a socket address.
* <p>
* This method performs a call to {@code open()} followed by a call to
* {@code connect(SocketAddress)}.
*
* @param address
* the socket address to be connected to.
* @return the new connected channel.
* @throws AsynchronousCloseException
* if this channel is closed by another thread while this method
* is executing.
* @throws ClosedByInterruptException
* if another thread interrupts the calling thread while this
* operation is executing. The calling thread will have the
* interrupt state set and the channel will be closed.
* @throws UnresolvedAddressException
* if the address is not resolved.
* @throws UnsupportedAddressTypeException
* if the address type is not supported.
* @throws IOException
* if an I/O error occurs.
*/
public static SocketChannel open(SocketAddress address) throws IOException {
SocketChannel socketChannel = open();
if (socketChannel != null) {
socketChannel.connect(address);
}
return socketChannel;
}
代码示例来源:origin: apache/activemq
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
SocketChannel channel = SocketChannel.open();
channel.connect(new InetSocketAddress(host, port));
return channel.socket();
}
代码示例来源:origin: apache/nifi
@Override
public void run() {
byte[] bytes = message.getBytes(Charset.forName("UTF-8"));
final ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
for (int i = 0; i < numMessages; i++) {
try (SocketChannel channel = SocketChannel.open()) {
channel.connect(new InetSocketAddress("localhost", port));
buffer.clear();
buffer.put(bytes);
buffer.flip();
while (buffer.hasRemaining()) {
channel.write(buffer);
}
Thread.sleep(delay);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
}
代码示例来源:origin: prestodb/presto
@Override
public Socket createSocket(String host, int port)
throws IOException
{
return SocketChannel.open(new InetSocketAddress(host, port)).socket();
}
代码示例来源:origin: apache/zookeeper
@Test
public void testClientReconnect() throws IOException, InterruptedException {
HostProvider hostProvider = mock(HostProvider.class);
when(hostProvider.size()).thenReturn(1);
InetSocketAddress inaddr = new InetSocketAddress("127.0.0.1", 1111);
when(hostProvider.next(anyLong())).thenReturn(inaddr);
ZooKeeper zk = mock(ZooKeeper.class);
when(zk.getClientConfig()).thenReturn(new ZKClientConfig());
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();
}
}
代码示例来源: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: prestodb/presto
@Override
public Socket createSocket()
throws IOException
{
return SocketChannel.open().socket();
}
代码示例来源:origin: org.mongodb/mongo-java-driver
isTrue("unopened", getChannel() == null);
try {
final SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(getServerAddress().getSocketAddress());
代码示例来源: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: killme2008/xmemcached
public Future<Boolean> connect(InetSocketAddressWrapper addressWrapper) {
if (addressWrapper == null) {
throw new NullPointerException("Null Address");
}
// Remove addr from removed set
this.removedAddrSet.remove(addressWrapper.getInetSocketAddress());
SocketChannel socketChannel = null;
ConnectFuture future = new ConnectFuture(addressWrapper);
try {
socketChannel = SocketChannel.open();
this.configureSocketChannel(socketChannel);
if (!socketChannel.connect(addressWrapper.getInetSocketAddress())) {
this.selectorManager.registerChannel(socketChannel, SelectionKey.OP_CONNECT, future);
} else {
this.addSession(this.createSession(socketChannel, addressWrapper));
future.setResult(true);
}
} catch (Exception e) {
if (socketChannel != null) {
try {
socketChannel.close();
} catch (IOException e1) {
//propagate original exception
}
}
future.failure(e);
}
return future;
}
内容来源于网络,如有侵权,请联系作者删除!