NioServer.java
package nio;
import bio.BioServerHandle;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class NioServer {
public static void main(String[] args) {
new NioServerHandle(1986).start();
try {
Thread.sleep(111111111111L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package nio;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
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;
public class NioServerHandle extends Thread {
private Socket socket;
private Selector selector;
private ServerSocketChannel serverSocketChannel;
public NioServerHandle(int port) {
try {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(1986), 1024);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println(" server init");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
selector.select(1000);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
SelectionKey key = null;
while (iterator.hasNext()){
key = iterator.next();
iterator.remove();
try {
dealWithKey(key);
} catch (Exception e) {
if (key != null) {
key.cancel();
if (key.channel() != null) {
key.channel().close();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
private void dealWithKey(SelectionKey key) throws Exception {
if (key.isValid()) {
if (key.isAcceptable()){
System.out.println(" server accept ");
ServerSocketChannel ssChannel = (ServerSocketChannel)key.channel();
SocketChannel socketChannel = ssChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
}
if (key.isReadable()){
System.out.println(" server read begin ");
SocketChannel socketChannel = (SocketChannel)key.channel();
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = socketChannel.read(readBuffer);
if (readBytes > 0){
readBuffer.flip();
byte[] bytes = new byte[readBuffer.remaining()];
readBuffer.get(bytes);
System.out.println("server read : " + new String(bytes));
String response = "server ack " + new String(bytes) ;
ByteBuffer writeBuffer = ByteBuffer.allocate(response.length());
writeBuffer.put(response.getBytes());
writeBuffer.flip();
while (writeBuffer.hasRemaining()){
try {
socketChannel.write(writeBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(" server writed ");
} else if (readBytes < 0 ){
System.out.println(" server cancel ");
key.channel();
socketChannel.close();
}
}
}
}
}
NioClient.java
package nio;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
public class NioClient {
public static void main(String[] args) {
try {
for (int i = 0; i < 100; i++) {
new NioClientHandle().start();
}
Thread.sleep(111111111111L);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
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.Collection;
import java.util.Iterator;
import java.util.Set;
public class NioClientHandle extends Thread {
private Selector selector;
private SocketChannel socketChannel;
boolean stop = false;
public NioClientHandle() {
try {
selector = Selector.open();
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
if (socketChannel.connect(new InetSocketAddress("127.0.0.1", 1986))){
socketChannel.register(selector, SelectionKey.OP_READ);
} else {
socketChannel.register(selector, SelectionKey.OP_CONNECT);
System.out.println(" client register");
}
} catch (IOException e) {
e.printStackTrace();
}
while (!stop){
try {
selector.select(1000);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
SelectionKey key = null;
while (iterator.hasNext()){
key = iterator.next();
try {
iterator.remove();
} catch (Exception e) {
System.out.println(" client remove error ");
e.printStackTrace();
}
dealWithKey(key);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
private void dealWithKey(SelectionKey key) throws Exception {
if (key.isValid()) {
SocketChannel socketChannel = (SocketChannel)key.channel();
if (key.isConnectable()){
System.out.println(" client connectable ");
if (socketChannel.finishConnect()){
System.out.println(" client finish connect ");
socketChannel.register(selector, SelectionKey.OP_READ);
write(socketChannel);
}
}
if (key.isReadable()){
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = socketChannel.read(readBuffer);
if (readBytes > 0){
readBuffer.flip();
byte[] bytes = new byte[readBuffer.remaining()];
readBuffer.get(bytes);
System.out.println("client read : " + new String(bytes));
stop = true;
}
}
} else {
System.out.println(" client key is invalid ");
}
}
private void write(SocketChannel socketChannel){
String sendMsg = Thread.currentThread().getName();
ByteBuffer writeBuffer = ByteBuffer.allocate(sendMsg.getBytes().length);
writeBuffer.put(sendMsg.getBytes());
writeBuffer.flip();
while (writeBuffer.hasRemaining()){
try {
socketChannel.write(writeBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(" client writed ");
}
}