package tk.andrew.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
/**
* @author dafuchen
* 2018/1/31
*/
public class Server {
private static final int SIZE = 1024;
public static void main(String[] args) {
try (ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
Selector selector = Selector.open()) {
serverSocketChannel.socket().bind(new InetSocketAddress( 8080));
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
Thread.sleep(1000L);
selector.select();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> selectionKeyIterator = selectionKeys.iterator();
while(selectionKeyIterator.hasNext()) {
SelectionKey selectionKey = selectionKeyIterator.next();
selectionKeyIterator.remove();
if (selectionKey.isValid()) {
if (selectionKey.isAcceptable()) {
ServerSocketChannel ssc = (ServerSocketChannel) selectionKey.channel();
SocketChannel socketChannel = ssc.accept();
socketChannel.configureBlocking(false);
socketChannel.write(ByteBuffer.wrap(new String("send a message from server").getBytes()));
socketChannel.register(selector, SelectionKey.OP_READ);
}
if (selectionKey.isReadable()) {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(SIZE);
int size = socketChannel.read(byteBuffer);
if (size >= 0) {
byte[] data = byteBuffer.array();
String msg = new String(data).trim();
System.out.println(msg);
socketChannel.write(ByteBuffer.wrap(new String("send a message from server").getBytes()));
} else {
socketChannel.close();
selectionKey.cancel();
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package tk.andrew.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
/**
* @author dafuchen
* 2018/2/1
*/
public class Client {
private static final int SIZE = 1024;
private static final String IP_ADDRESS = "127.0.0.1";
private static final int PORT = 8080;
public static void main(String[] args) throws IOException, InterruptedException {
SocketChannel socketChannel = SocketChannel.open();
Selector selector = Selector.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress(IP_ADDRESS, PORT));
socketChannel.register(selector, SelectionKey.OP_CONNECT);
while(true) {
Thread.sleep(1000);
selector.select();
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
iterator.remove();
if (selectionKey.isValid()) {
if (selectionKey.isConnectable()) {
SocketChannel sc = (SocketChannel) selectionKey.channel();
if (sc.isConnectionPending()) {
sc.finishConnect();
}
sc.configureBlocking(false);
sc.write(ByteBuffer.wrap(new String("向服务端发送了一条信息").getBytes()));
sc.register(selector, SelectionKey.OP_READ);
} else {
if (selectionKey.isReadable()) {
SocketChannel sc = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(SIZE);
int size = sc.read(byteBuffer);
if (size >= 0) {
byte[] data = byteBuffer.array();
String msg = new String(data).trim();
System.out.println(msg);
sc.write(ByteBuffer.wrap(new String("向服务端发送了一条信息").getBytes()));
} else {
sc.close();
selectionKey.channel();
}
}
}
}
}
}
}
}
代码抄袭自[NIO与传统IO的区别]