在这个例子上做的修改:http://blog.csdn.net/eclipser1987/article/details/7329362
Server.java
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.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
/**
* Created by yangankang on 16/2/20.
*/
public class Server implements Runnable {
private int port = 8888;
private ServerSocketChannel socketChannel;
private SocketChannel clientChannel;
private Selector selector;
private ByteBuffer byteBuffer = ByteBuffer.allocate(512);
public Server() {
init();
}
public void init() {
try {
this.selector = SelectorProvider.provider().openSelector();
this.socketChannel = ServerSocketChannel.open();
this.socketChannel.configureBlocking(false);
this.socketChannel.socket().bind(new InetSocketAddress("localhost", port));
this.socketChannel.register(this.selector, SelectionKey.OP_ACCEPT);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Server server = new Server();
Thread thread = new Thread(server);
thread.start();
}
@Override
public void run() {
while (true) {
try {
System.err.println("Server Start.");
this.selector.select();
Iterator iterator = this.selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = (SelectionKey) iterator.next();
iterator.remove();
if (!key.isValid()) {
continue;
}
if (key.isAcceptable()) {
this.accept(key);
} else if (key.isReadable()) {
this.read(key);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void accept(SelectionKey key) {
ServerSocketChannel socketChannel = (ServerSocketChannel) key.channel();
try {
clientChannel = socketChannel.accept();
clientChannel.configureBlocking(false);
clientChannel.register(this.selector, SelectionKey.OP_READ);
} catch (IOException e) {
e.printStackTrace();
}
}
public void read(SelectionKey key) {
this.byteBuffer.clear();
SocketChannel socketChannel = (SocketChannel) key.channel();
try {
int count = socketChannel.read(this.byteBuffer);
if (count == -1) {
key.channel().close();
key.channel();
return;
}
String string = new String(this.byteBuffer.array()).trim();
System.out.println("from client message:" + string);
socketChannel.write(ByteBuffer.wrap(("you say:" + string).getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
/**
* Created by yangankang on 16/2/21.
*/
public class Client {
private ByteBuffer buffer = ByteBuffer.allocate(512);
private String url = "localhost";
private int port = 8888;
public void send() {
InetSocketAddress address = new InetSocketAddress(url, port);
SocketChannel socketChannel = null;
while (true) {
byte[] b = new byte[512];
try {
System.err.print("input your message:");
System.in.read(b);
socketChannel = SocketChannel.open();
socketChannel.connect(address);
buffer.clear();
buffer.put(b);
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
socketChannel.configureBlocking(true);
ByteBuffer readBuffer = ByteBuffer.allocate(512);
socketChannel.read(readBuffer);
System.out.println("from client message:" + (new String(readBuffer.array()).trim()));
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socketChannel != null)
try {
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
Client client = new Client();
client.send();
}
}