package NonBlockingSocket;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
public class Server {// 这是服务端
/**
* @param args
* @throws IOException
*
* 非阻塞的Socked使用总结(服务器端): 一、涉及关键类: 1、ByteBuffer 2、SelectionKey 3、Selector
* 4、ServerSocketChannel 5、Iterator 6、SocketChannel 二、各类的综合使用方法及相互联系
* 以下是使用前对各对象的设置: 1、得到ServerSocketChannel和Selector对象
* 2、设置ServerSocketChannel内部channel阻塞方式 3、把ServerSocketChannel绑定到指定地址的指定端口上
* 4、把ServerSocketChannel注册给Selector对象,并选择感兴趣的动作 以下是使用过程:
* 1、使用Selector对象对指定地址指定端口的请求进行选择,这里的选择只关注自己以前设置的感兴趣的请求
* 2、从Selector对象里得到感兴趣的链接的Set,然后得到该Set对象的迭代器Iterator对象
* 3、如果该迭代器里有元素,则依次使用next()取出,取出的元素可强转为SelectionKey,此SelectionKey对象至少包装了两个属性:一个是该属性是哪种类型的,另一个是一个SelectableChannel
* 如果此SelectedKey是OP_ACCEPT,那么SelectableChannel可以强转为ServerSocketChannel对象,使这个对象accept()可以得到SocketChannel对象,同样可以像注册ServerSocketChannel
* 一样注册SocketChannel到Selector对像并选择自己感兴趣的动作。SelectionKey的isConnecttable()方法不适用于服务端。
* 4、可以从SocketChannel内read()和write()信息,这些信息必须包装在ByteBuffer内
* 5、可以对ByteBuffer里的内容做多种解释,其中一种是人类比较容易理解的字符格式:CharBuffer,转换方式有两种:一种是ByteBuffer.asCharBuffer()这种转换只是改变了相同内容的不同解释;
* 另一种是使用编码: Charset tCs=Charset.forName("gb2312"); CharBuffer
* tCb=tCs.newDecoder().decode(tBb); 这种转换是对相同语义做不同存储。
* 6、经常将上述步骤放在一个大的循环内部,这样可以不断地检查指定地址的指定端口的请求
*/
private final static int BUF_SIZE = 1024;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Server s = new Server();
s.startServer();
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//
public Server() {
// TODO Auto-generated constructor stub
init();
}
ServerSocketChannel ssc;
Selector sel;
boolean start;
//
private void init() {
// TODO Auto-generated method stub
try {
ssc = ServerSocketChannel.open();
ssc.socket()
.bind(
new InetSocketAddress(InetAddress
.getByAddress(new byte[] { (byte) 11,
(byte) 11, (byte) 11, (byte) 11 }),
1111));
sel = Selector.open();
ssc.configureBlocking(false);
ssc.register(sel, SelectionKey.OP_ACCEPT);
start = false;
aid = 0;
rid = 0;
wid = 0;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static int rid;
private static int wid;
private static int aid;
//
private void startServer() throws IOException {
// TODO Auto-generated method stub
start = true;
Iterator<SelectionKey> iKey = null;
SelectionKey skKey = null;
System.out.println("Server started.");
while (start) {
sel.select();// 不要忘了
iKey = sel.selectedKeys().iterator();
if (iKey.hasNext()) {
// System.out.println("Server=====>>>hasNext");
skKey = iKey.next();
iKey.remove();
if (skKey.isAcceptable()) {
aid++;
ServerSocketChannel tSsc = (ServerSocketChannel) skKey
.channel();
SocketChannel tSc = tSsc.accept();
tSc.configureBlocking(false);
tSc.register(sel, SelectionKey.OP_READ
| SelectionKey.OP_WRITE);
System.out.println("The " + aid + "th accept.");
continue;
}
if (skKey.isReadable()) {
rid++;
SocketChannel tSc = (SocketChannel) skKey.channel();
tSc.configureBlocking(false);
// ByteBuffer的创建方法之一:ByteBuffer.allocate(BUF_SIZE);
ByteBuffer tBb = ByteBuffer.allocate(BUF_SIZE);
tSc.read(tBb);
tBb.flip();
// 以下做解码用:
Charset tCs = Charset.forName("gb2312");
CharBuffer tCb = tCs.newDecoder().decode(tBb);
System.out.println(tCb);
System.out.println("The " + rid + "th read.");
continue;
}
if (skKey.isWritable()) {
wid++;
SocketChannel tSc = (SocketChannel) skKey.channel();
tSc.configureBlocking(false);
// CharBuffer tCb=ByteBuffer.wrap(("The "+wid+"th
// response.").getBytes()).asCharBuffer();
// ByteBuffer的创建方法之二:ByteBuffer.wrap(("The "+wid+"th
// response.").getBytes())
tSc.write(ByteBuffer.wrap(("The " + wid + "th response.")
.getBytes()));
System.out.println("The " + wid + "th write.");
continue;
}
}
}
}
}