請問一個java nio問題
在學習一本java網路程式設計的書的時候有一個程式看不懂,程式如下,
Example 12-2. A non-blocking chargen server
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
import java.io.IOException;
public class ChargenServer {
public static int DEFAULT_PORT = 19;
public static void main(String[] args) {
int port;
try {
port = Integer.parseInt(args[0]);
}
catch (Exception ex) {
port = DEFAULT_PORT;
}
System.out.println("Listening for connections on port " + port);
byte[] rotation = new byte[95*2];
for (byte i = ' '; i <= '~'; i++) {
rotation[i-' '] = i;
rotation[i+95-' '] = i;
}
ServerSocketChannel serverChannel;
Selector selector;
try {
serverChannel = ServerSocketChannel.open( );
ServerSocket ss = serverChannel.socket( );
InetSocketAddress address = new InetSocketAddress(port);
ss.bind(address);
serverChannel.configureBlocking(false);
selector = Selector.open( );
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}
catch (IOException ex) {
ex.printStackTrace( );
return;
}
while (true) {
try {
selector.select( );
}
catch (IOException ex) {
ex.printStackTrace( );
break;
}
Set readyKeys = selector.selectedKeys( );
Iterator iterator = readyKeys.iterator( );
while (iterator.hasNext( )) {
SelectionKey key = (SelectionKey) iterator.next( );
iterator.remove( );
try {
if (key.isAcceptable( )) {
ServerSocketChannel server = (ServerSocketChannel) key.channel( );
SocketChannel client = server.accept( );
System.out.println("Accepted connection from " + client);
client.configureBlocking(false);
SelectionKey key2 = client.register(selector, SelectionKey.
OP_WRITE);
ByteBuffer buffer = ByteBuffer.allocate(74);
buffer.put(rotation, 0, 72);
buffer.put((byte) '\r');
buffer.put((byte) '\n');
buffer.flip( );
key2.attach(buffer);
}
else if (key.isWritable( )) {
SocketChannel client = (SocketChannel) key.channel( );
ByteBuffer buffer = (ByteBuffer) key.attachment( );
if (!buffer.hasRemaining( )) {
// Refill the buffer with the next line
buffer.rewind( );
// Get the old first character
int first = buffer.get( );
// Get ready to change the data in the buffer
buffer.rewind( );
// Find the new first characters position in rotation
int position = first - ' ' + 1;
// copy the data from rotation into the buffer
buffer.put(rotation, position, 72);
// Store a line break at the end of the buffer
buffer.put((byte) '\r');
buffer.put((byte) '\n');
// Prepare the buffer for writing
buffer.flip( );
}
client.write(buffer);
}
}
catch (IOException ex) {
key.cancel( );
try {
key.channel( ).close( );
}
catch (IOException cex) {}
}
}
}
}
}
請問
1是否可以多個channel在selector中註冊?
2這個程式那裡體現了使用java nio的好處,這個程式的目的是什麼?
謝謝
Example 12-2. A non-blocking chargen server
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
import java.io.IOException;
public class ChargenServer {
public static int DEFAULT_PORT = 19;
public static void main(String[] args) {
int port;
try {
port = Integer.parseInt(args[0]);
}
catch (Exception ex) {
port = DEFAULT_PORT;
}
System.out.println("Listening for connections on port " + port);
byte[] rotation = new byte[95*2];
for (byte i = ' '; i <= '~'; i++) {
rotation[i-' '] = i;
rotation[i+95-' '] = i;
}
ServerSocketChannel serverChannel;
Selector selector;
try {
serverChannel = ServerSocketChannel.open( );
ServerSocket ss = serverChannel.socket( );
InetSocketAddress address = new InetSocketAddress(port);
ss.bind(address);
serverChannel.configureBlocking(false);
selector = Selector.open( );
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}
catch (IOException ex) {
ex.printStackTrace( );
return;
}
while (true) {
try {
selector.select( );
}
catch (IOException ex) {
ex.printStackTrace( );
break;
}
Set readyKeys = selector.selectedKeys( );
Iterator iterator = readyKeys.iterator( );
while (iterator.hasNext( )) {
SelectionKey key = (SelectionKey) iterator.next( );
iterator.remove( );
try {
if (key.isAcceptable( )) {
ServerSocketChannel server = (ServerSocketChannel) key.channel( );
SocketChannel client = server.accept( );
System.out.println("Accepted connection from " + client);
client.configureBlocking(false);
SelectionKey key2 = client.register(selector, SelectionKey.
OP_WRITE);
ByteBuffer buffer = ByteBuffer.allocate(74);
buffer.put(rotation, 0, 72);
buffer.put((byte) '\r');
buffer.put((byte) '\n');
buffer.flip( );
key2.attach(buffer);
}
else if (key.isWritable( )) {
SocketChannel client = (SocketChannel) key.channel( );
ByteBuffer buffer = (ByteBuffer) key.attachment( );
if (!buffer.hasRemaining( )) {
// Refill the buffer with the next line
buffer.rewind( );
// Get the old first character
int first = buffer.get( );
// Get ready to change the data in the buffer
buffer.rewind( );
// Find the new first characters position in rotation
int position = first - ' ' + 1;
// copy the data from rotation into the buffer
buffer.put(rotation, position, 72);
// Store a line break at the end of the buffer
buffer.put((byte) '\r');
buffer.put((byte) '\n');
// Prepare the buffer for writing
buffer.flip( );
}
client.write(buffer);
}
}
catch (IOException ex) {
key.cancel( );
try {
key.channel( ).close( );
}
catch (IOException cex) {}
}
}
}
}
}
請問
1是否可以多個channel在selector中註冊?
2這個程式那裡體現了使用java nio的好處,這個程式的目的是什麼?
謝謝
相關文章
- Java NIO 執行緒 的一個問題Java執行緒
- 請教一個關於NIO執行方式的問題
- NIO Socket的2個問題?請高手幫忙!
- 請教大家一個JAVA的問題Java
- Mr bangq請教你一個java列印的問題!Java
- 請各路高手幫忙,關於JAVA NIO的方面的問題Java
- 關於Java NIO的一些問題,求助。Java
- 請教一個JVM問題JVM
- 請教高手一個問題!
- java nio網路開發問題Java
- 請教 NIO 中物件流通訊的問題物件
- 【求助】請教一個初學java的小問題Java
- 請問在JAVA WEB專案中一個比較棘手的問題JavaWeb
- 請問一個有關jdbc效能的問題JDBC
- 請問一個jndi連線的小問題
- 請教一個cookies的問題Cookie
- 請教一個executeBatch()的問題BAT
- 請教各位高手一個問題
- java nio解決半包 粘包問題Java
- 我是剛開學JAVA,想請教一個問題。Java
- 求問一個 swipe 的問題,請大神們指教
- 請問一個mysql使用者管理的 問題。MySql
- 請教一個struct tag的問題Struct
- 請教一個演算法問題演算法
- 請教一個observer設計問題。Server
- 請教一個ADAPTER的問題APT
- 請教一個WEB START的問題!Web
- 請教伺服器是NIO的socket server問題?伺服器Server
- 請教一個java程式記憶體釋放的問題Java記憶體
- 問一個小的運算問題,請高手指教!
- 請問老師一個關於命令模式的問題模式
- 請教一個go切片引用的問題Go
- 請教一個物件設計的問題物件
- 求教 一個java取php session的問題 急 請板橋里人指教~~~JavaPHPSession
- 請教一個關於JAVA SOCKET程式設計中的問題Java程式設計
- 一個java加密引起的問題Java加密
- 一個小問題 TestClient.javaclientJava
- 學java不久,傻瓜問題一個Java