use of java.nio.channels.SelectionKey in project runesource by PureCS.
the class Server method accept.
/**
* Accepts any incoming connections.
*
* @throws IOException
*/
private void accept() throws IOException {
SocketChannel socket;
/*
* Here we use a for loop so that we can accept multiple clients per
* tick for lower latency. We limit the amount of clients that we
* accept per tick to better combat potential denial of service type
* attacks.
*/
for (int i = 0; i < 10; i++) {
socket = serverChannel.accept();
if (socket == null) {
// No more connections to accept (as this one was invalid).
break;
}
// Register the connection
HostGateway.enter(socket.socket().getInetAddress().getHostAddress());
// Set up the new connection.
socket.configureBlocking(false);
SelectionKey key = socket.register(selector, SelectionKey.OP_READ);
Client client = new Player(key);
System.out.println("Accepted " + client + ".");
getClientMap().put(key, client);
}
}
use of java.nio.channels.SelectionKey in project yyl_example by Relucent.
the class Receive method main.
public static void main(String[] args) throws Exception {
ByteBuffer buffer = ByteBuffer.allocate(1024);
ServerSocketChannel ss = ServerSocketChannel.open();
ss.socket().bind(new InetSocketAddress(9998));
ss.configureBlocking(false);
Selector se = Selector.open();
ss.register(se, SelectionKey.OP_ACCEPT);
while (se.select() > 0) {
Set<SelectionKey> set = se.selectedKeys();
for (SelectionKey key : set) {
System.out.println("-------------");
SocketChannel sc = null;
try {
if (key.isAcceptable()) {
sc = ss.accept();
System.err.println("ACCEPTABLE:" + sc.socket());
sc.configureBlocking(false);
sc.register(se, SelectionKey.OP_READ);
} else if (key.isReadable()) {
sc = (SocketChannel) key.channel();
System.out.println(sc.isConnected());
sc.read(buffer);
buffer.flip();
System.out.println(new String(buffer.array(), 0, buffer.remaining()));
sc.write(ByteBuffer.wrap("+".getBytes()));
}
} catch (Exception e) {
key.cancel();
if (sc != null) {
sc.close();
}
e.printStackTrace();
}
}
set.clear();
}
}
use of java.nio.channels.SelectionKey in project yyl_example by Relucent.
the class Send method main.
public static void main(String[] args) throws Exception {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
Selector selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);
socketChannel.connect(new InetSocketAddress("127.0.0.1", 9998));
while (!socketChannel.finishConnect()) {
}
ByteBuffer buffer = ByteBuffer.allocate(1024);
socketChannel.write(ByteBuffer.wrap("-".getBytes()));
int index = 0;
while (true) {
if (selector.select() == 0) {
Thread.sleep(1000);
continue;
}
Set<SelectionKey> set = selector.selectedKeys();
for (SelectionKey key : set) {
int ops = key.readyOps();
if ((ops & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT) {
socketChannel.write(buffer);
System.out.println("OP_CONNECT:");
}
if ((ops & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
socketChannel.read(buffer);
buffer.flip();
System.out.println(new String(buffer.array(), 0, buffer.remaining()));
buffer.clear();
socketChannel.write(ByteBuffer.wrap(("No-" + index + " | ").getBytes()));
index++;
}
}
set.clear();
Thread.sleep(1000);
}
}
use of java.nio.channels.SelectionKey in project CshBBrain by CshBBrain.
the class Worker method process.
public void process(Object selectKey) {
if (selectKey instanceof SelectionKey) {
SelectionKey selectionKey = (SelectionKey) selectKey;
Client key = Client.getSockector(selectionKey);
if (key.getMessages(charBuffer)) {
// 已完成握手,从客户端读取报刊
// 进行业务处理
key.process();
}
} else if (selectKey instanceof Client) {
// 直接发送消息
((Client) selectKey).send();
}
}
use of java.nio.channels.SelectionKey in project CshBBrain by CshBBrain.
the class MasterServer method startClustersMonitor.
/**
*
* <li>方法名:startClustersMonitor
* <li>
* <li>返回类型:void
* <li>说明:开始监听集群服务器节点中的连接请求
* <li>创建人:CshBBrain, 技术博客:http://cshbbrain.iteye.com/
* <li>创建日期:2012-10-21
* <li>修改人:
* <li>修改日期:
*/
private void startClustersMonitor() {
try {
// Create a new selector
Selector selector = Selector.open();
// Create a new non-blocking server socket channel
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
// 集群端口
InetSocketAddress isa = new InetSocketAddress(this.clustersPort);
serverChannel.socket().bind(isa);
// Register the server socket channel, indicating an interest in
// accepting new connections
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
log.info("集群服务器准备就绪,等待集群请求到来");
while (noStopRequested) {
try {
int num = 0;
// Wait for an event one of the registered channels
num = selector.select(100);
if (num > 0) {
// Iterate over the set of keys for which events are available
Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
while (selectedKeys.hasNext()) {
SelectionKey key = (SelectionKey) selectedKeys.next();
selectedKeys.remove();
if (!key.isValid()) {
continue;
}
// Check what event is available and deal with it
if (key.isAcceptable()) {
// 注册集群连接
this.accept(key, true);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 关闭服务器
serverChannel.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Clusters Server start up fail");
}
}
Aggregations