use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class InterruptedStreamTest method newSocketChannelPair.
/**
* Returns a pair of connected sockets backed by NIO socket channels.
*/
private Socket[] newSocketChannelPair() throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(0));
SocketChannel clientSocketChannel = SocketChannel.open();
clientSocketChannel.connect(serverSocketChannel.socket().getLocalSocketAddress());
SocketChannel server = serverSocketChannel.accept();
serverSocketChannel.close();
return new Socket[] { clientSocketChannel.socket(), server.socket() };
}
use of java.nio.channels.ServerSocketChannel in project CloudStack-archive by CloudStack-extras.
the class NioServer method init.
@Override
protected void init() throws IOException {
_selector = SelectorProvider.provider().openSelector();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
_localAddr = new InetSocketAddress(_port);
ssc.socket().bind(_localAddr);
ssc.register(_selector, SelectionKey.OP_ACCEPT, null);
s_logger.info("NioConnection started and listening on " + _localAddr.toString());
}
use of java.nio.channels.ServerSocketChannel in project CshBBrain by CshBBrain.
the class MasterServer method accept.
/**
*
* <li>方法名:accept
* <li>@param key
* <li>@param isClusters
* <li>@throws IOException
* <li>返回类型:void
* <li>说明:根据请求创建连接,可以是来自客户端的请求,也可能是来自集群中其他服务器的请求
* <li>创建人:CshBBrain, 技术博客:http://cshbbrain.iteye.com/
* <li>创建日期:2012-10-21
* <li>修改人:
* <li>修改日期:
*/
private void accept(SelectionKey key, Boolean isClusters) throws IOException {
// For an accept to be pending the channel must be a server socket channel.
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
// Accept the connection and make it non-blocking
SocketChannel socketChannel = serverSocketChannel.accept();
// 设置为非阻塞式
socketChannel.configureBlocking(false);
// 设置接收缓存
socketChannel.socket().setReceiveBufferSize(this.sockectReceiveBufferSize * 1024);
// 设置发送缓存
socketChannel.socket().setSendBufferSize(this.sockectSendBufferSize * 1024);
socketChannel.socket().setSoTimeout(0);
socketChannel.socket().setTcpNoDelay(true);
if (keepConnect) {
//使用长连接,保持连接状态
socketChannel.socket().setKeepAlive(true);
}
// 将客户端sockect通道注册到指定的输入监听线程上
if (isClusters) {
// 集群请求通道注册
this.readWriteMonitors.get(Math.abs(this.connectIndex.getAndIncrement()) % this.readWriteMonitors.size()).registeClusters(socketChannel);
} else {
// 业务请求通道注册
this.readWriteMonitors.get(Math.abs(this.connectIndex.getAndIncrement()) % this.readWriteMonitors.size()).registe(socketChannel);
}
}
use of java.nio.channels.ServerSocketChannel in project CloudStack-archive by CloudStack-extras.
the class NioConnection method accept.
protected void accept(SelectionKey key) throws IOException {
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
Socket socket = socketChannel.socket();
socket.setKeepAlive(true);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Connection accepted for " + socket);
}
// Begin SSL handshake in BLOCKING mode
socketChannel.configureBlocking(true);
SSLEngine sslEngine = null;
try {
SSLContext sslContext = Link.initSSLContext(false);
sslEngine = sslContext.createSSLEngine();
sslEngine.setUseClientMode(false);
sslEngine.setNeedClientAuth(false);
Link.doHandshake(socketChannel, sslEngine, false);
} catch (Exception e) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Socket " + socket + " closed on read. Probably -1 returned: " + e.getMessage());
}
try {
socketChannel.close();
socket.close();
} catch (IOException ignore) {
}
return;
}
if (s_logger.isTraceEnabled()) {
s_logger.trace("SSL: Handshake done");
}
socketChannel.configureBlocking(false);
InetSocketAddress saddr = (InetSocketAddress) socket.getRemoteSocketAddress();
Link link = new Link(saddr, this);
link.setSSLEngine(sslEngine);
link.setKey(socketChannel.register(key.selector(), SelectionKey.OP_READ, link));
Task task = _factory.create(Task.Type.CONNECT, link, null);
registerLink(saddr, link);
_executor.execute(task);
}
use of java.nio.channels.ServerSocketChannel in project spring-boot by spring-projects.
the class TunnelClient method start.
/**
* Start the client and accept incoming connections on the port.
* @throws IOException in case of I/O errors
*/
public void start() throws IOException {
synchronized (this.monitor) {
Assert.state(this.serverThread == null, "Server already started");
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(this.listenPort));
logger.trace("Listening for TCP traffic to tunnel on port " + this.listenPort);
this.serverThread = new ServerThread(serverSocketChannel);
this.serverThread.start();
}
}
Aggregations