- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我用 Java 构建了一个基于选择器的系统,可以接受多个客户端。它有一个在 OP_ACCEPT 下注册的 ServerSocketChannel,它接受传入的连接并再次向选择器注册生成的 SocketChannel。这是一点:
ServerSocketChannel insock = ServerSocketChannel.open();
insock.configureBlocking(false);
insock.socket().bind(new InetSocketAddress(6789));
Selector sel = Selector.open();
SelectionKey joinchannel = insock.register(sel, SelectionKey.OP_ACCEPT);
System.out.println("Ready to accept incoming connections.");
while (true) {
int ready = sel.selectNow();
if (ready == 0)
continue;
Set<SelectionKey> selectedKeys = sel.selectedKeys();
Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if(key.isAcceptable()){
SocketChannel newConnection = insock.accept();
System.out.println("New client "+newConnection+" connected.");
newConnection.configureBlocking(false);
newConnection.register(sel, SelectionKey.OP_READ).attach(new DGPlayer());
}
如果我为 OP_READ 注册新的 SocketChannel,这工作正常。 isReadable() 检查成功,读取数据。这是一点:
else if(key.isReadable()){
ByteBuffer buf = ByteBuffer.allocate(1024);
int trans = ((SocketChannel)key.channel()).read(buf); buf.flip();
byte[] ba = new byte[buf.remaining()]; buf.get(ba);
String msg = new String(ba, 0, ba.length, Charset.forName("UTF-8"));
if(trans > 0){
DGPlayer client = (DGPlayer) key.attachment();
System.out.println(client.name+": "+msg.trim());
}
}
// else if(key.isWritable()){
// ByteBuffer buf = ByteBuffer.allocate(48);
// buf.clear();
// buf.put(((String)key.attachment()).getBytes());
// buf.flip();
//
// SocketChannel target = ((SocketChannel)key.channel());
// while(buf.hasRemaining()) {
// target.write(buf);
// }
//
// key.attach(null);
// }
但是,如果我为 OP_READ|OP_WRITE 注册 SocketChannel,则什么也没有发生。据我所知,选择器从未发现 channel 是 readable() 或 writable()。唯一的变化是注册 channel 以获得 WRITE 权限。
知道为什么会这样吗?
编辑 - 为了完成,这里有一些客户端代码:
while (true) {
int readyChannels = sel.select();
if (readyChannels == 0)
continue;
Set<SelectionKey> selectedKeys = sel.selectedKeys();
Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (key.isReadable()) {
ByteBuffer buf = ByteBuffer.allocate(1024);
int trans = ((SocketChannel)key.channel()).read(buf); buf.flip();
byte[] ba = new byte[buf.remaining()]; buf.get(ba);
String msg = new String(ba, 0, ba.length, Charset.forName("UTF-8"));
if(msg.trim().length() != 0)
System.out.println("Response from server: "+msg.trim());
}
else if(key.isWritable() && messages.size() > 0){
String message = messages.remove();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(message.getBytes(Charset.forName("UTF-8")));
buf.flip();
int written = outsock.write(buf);
while(buf.hasRemaining()){
outsock.write(buf);
}
System.out.println("Wrote '"+message+"' to server");
}
keyIterator.remove();
}
}
(客户端为 OP_READ|OP_WRITE 注册服务器)
最佳答案
一般来说,如果您没有任何要主动写入的内容,那么为 OP_WRITE
注册一个 channel 是个坏主意。大多数 channel 大部分时间都可以写入,因此每次调用 Selector.select
都会无阻塞地返回并消耗资源。最好在SelectionKey.interestOps
中加上writing flag当您准备好向 channel 写入内容时,并在完成后删除标志。
关于从未选择用于多个 OP 代码的 Java SocketChannel register(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10940654/
背景 我最近在 merge 期间遇到了一个意外未 merge 的文档文件的问题。 无论出于何种原因,我搞砸了 merge 并有效地删除了文件(和其他几个文件),因为我忘记了它们的存在。 现在我想查看我
我在我的网站上使用旧的 mysql 版本和 php 版本 4。 我的表结构: | orders_status_history_id | orders_id | orders_status_id |
我是一名优秀的程序员,十分优秀!