一、服务端
package test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
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 NioServer {
private static ByteBuffer sendByte = ByteBuffer.allocate(1024);
private static ByteBuffer receByte = ByteBuffer.allocate(1024);
public static void main(String[] args) throws IOException {
System.out.println("服务器端已启动");
ServerSocketChannel channel = ServerSocketChannel.open();
SocketAddress address = new InetSocketAddress(8888);
channel.bind(address);
channel.configureBlocking(false);
Selector selector = Selector.open();
channel.register(selector, SelectionKey.OP_ACCEPT);
while(selector.select() > 0){
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while(iterator.hasNext()){
SelectionKey key = iterator.next();
iterator.remove();
if(key.isAcceptable()){
ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
SocketChannel clientChannel = serverChannel.accept();
if(clientChannel != null){
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_READ);
}
}else if(key.isReadable()){
SocketChannel clientChannel = (SocketChannel) key.channel();
receByte.clear();
int len = 0;
try {
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_WRITE);
len = clientChannel.read(receByte);
if(len > 0){
String receStr = new String(receByte.array(), 0, len, "utf-8");
// 打印客户端发送的数据
System.out.println(clientChannel.getRemoteAddress() + " : " + receStr);
}
} catch (IOException e) {
System.out.println("客户端断开连接");
key.cancel();
if(clientChannel != null)
clientChannel.close();
}
} else if (key.isWritable()) {
SocketChannel clientChannel = (SocketChannel) key.channel();
sendByte.clear();
String sendStr = "服务器已接收消息 ";
sendByte.put(sendStr.getBytes("utf-8"));
sendByte.flip();
clientChannel.write(sendByte);
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_READ);
}
}
}
}
}
二、客户端
nio
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class NioClient {
private static ByteBuffer sendByte = ByteBuffer.allocate(1024);
private static ByteBuffer receByte = ByteBuffer.allocate(1024);
public static void main(String[] args) throws IOException {
SocketChannel channel = SocketChannel.open();
SocketAddress address = new InetSocketAddress("127.0.0.1", 8888);
channel.connect(address);
channel.configureBlocking(false);
Selector selector = Selector.open();
channel.register(selector, SelectionKey.OP_WRITE);
while(selector.select() > 0){
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> iterator = keys.iterator();
while(iterator.hasNext()){
SelectionKey key = iterator.next();
iterator.remove();
if(key.isReadable()){
receByte.clear();
SocketChannel client = (SocketChannel) key.channel();
client.configureBlocking(false);
int len = client.read(receByte);
if(len > 0){
String receStr = new String(receByte.array(), 0, len, "utf-8");
System.out.println(receStr);
}
client.register(selector, SelectionKey.OP_WRITE);
}else if(key.isWritable()){
SocketChannel client = (SocketChannel) key.channel();
client.configureBlocking(false);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
if(line != null){
sendByte.clear();
sendByte.put(line.getBytes("utf-8"));
sendByte.flip();
client.write(sendByte);
}
client.register(selector, SelectionKey.OP_READ);
}
}
}
}
}
bio
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class BioClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream os = null;
InputStream is = null;
while(true){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
os = socket.getOutputStream();
os.write(line.getBytes());
os.flush();
is = socket.getInputStream();
byte[] b = new byte[1024];
int len = is.read(b);
if(len > 0){
String ret = new String(b, 0, len, "utf8");
System.out.println(ret);
}
if("over".equals(line)){
is.close();
os.close();
socket.close();
break;
}
}
}
}