java nio Demo:
NIOServer:
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;
public class NIOServer {
private Selector selector;
public void initServer(int port) throws IOException{
ServerSocketChannel serverChannel=ServerSocketChannel.open();
serverChannel.configureBlocking(false);
serverChannel.socket().bind(new InetSocketAddress(port));
this.selector=Selector.open();
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}
public void listen() throws IOException{
System.out.println("服务器启动");
while(true){
selector.select();
Iterator<SelectionKey> it=this.selector.selectedKeys().iterator();
while(it.hasNext()){
SelectionKey key=it.next();
it.remove();
if(key.isAcceptable()){
ServerSocketChannel server= (ServerSocketChannel) key.channel();
SocketChannel channel=server.accept();
channel.configureBlocking(false);
channel.write(ByteBuffer.wrap(new String("向服务端发送一条消息").getBytes()));
channel.register(selector,SelectionKey.OP_READ);
}else if(key.isReadable()){
readKey(key);
}
}
}
}
private void readKey(SelectionKey key) throws IOException {
SocketChannel channel=(SocketChannel) key.channel();
ByteBuffer buf=ByteBuffer.allocate(256);
channel.read(buf);
byte[] msg=buf.array();
String message=new String(msg).trim();
System.out.println("服务器收到消息:"+message);
ByteBuffer backBuf=ByteBuffer.wrap(message.getBytes());
channel.write(backBuf);
}
public static void main(String[] args) throws IOException {
NIOServer server=new NIOServer();
server.initServer(8000);
server.listen();
}
}
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;
public class NIOClient {
private Selector selector;
public void initClient(String ip,int port) throws IOException{
SocketChannel channel=SocketChannel.open();
channel.configureBlocking(false);
this.selector=Selector.open();
channel.connect(new InetSocketAddress(ip,port));
channel.register(selector,SelectionKey.OP_CONNECT);
}
public void listen() throws IOException{
while(true){
selector.select();
Iterator<SelectionKey> it=selector.selectedKeys().iterator();
while(it.hasNext()){
SelectionKey key=it.next();
it.remove();
if(key.isConnectable()){
SocketChannel channel=(SocketChannel) key.channel();
if(channel.isConnectionPending()){
channel.finishConnect();
}
channel.configureBlocking(false);
channel.write(ByteBuffer.wrap(new String("向服务器发送一条信息").getBytes()));
channel.register(selector,SelectionKey.OP_READ);
}else if(key.isReadable()){
read(key);
}
}
}
}
private void read(SelectionKey key) throws IOException {
SocketChannel channel=(SocketChannel) key.channel();
ByteBuffer buf=ByteBuffer.allocate(256);
channel.read(buf);
String str=new String(buf.array()).trim();
System.out.println("收到服务器信息:"+str);
}
public static void main(String[] args) throws IOException {
NIOClient nioClient=new NIOClient();
nioClient.initClient("localhost", 8000);
nioClient.listen();
}
}