NIO test Server.java import java.io.IOException;import java.net.InetSocketAddress;import java.net.SocketAddress;import java.nio.ByteBuffer;import java.nio.channels.*;import java.util.Iterator;import java.util.LinkedList;import java.util.Set
testServer.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import static java.nio.channels.SelectionKey.*;
public class Server {
private Selector selector;
private SocketAddress address ;
private ServerSocketChannel server;
private LinkedList
messQue = new LinkedList<>();//消息存储机制
public Server init(int port) throws IOException {
//初始化接口
address = new InetSocketAddress(port);
//注册多路复用选择器
selector = Selector.open();
//创建服务端对象
server = ServerSocketChannel.open();
//服务端
server.configureBlocking(false);
//像多路复用器注册
server.register(selector, OP_ACCEPT);
//监听端口
server.bind(address);
return this;
}
public void accept() throws IOException {
while (true) {
selector.select();
Set
keys = selector.selectedKeys(); Iterator
iterator = keys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); iterator.remove(); if (key.isAcceptable()) { ServerSocketChannel channel = (ServerSocketChannel) key.channel(); SocketChannel accept = channel.accept(); accept.configureBlocking(false); accept.register(selector, OP_READ, ByteBuffer.allocate(1024)); System.out.println("有客户端连接"); } if (key.isReadable()) { SocketChannel channel = (SocketChannel) key.channel(); ByteBuffer buf = (ByteBuffer) key.attachment(); int read = channel.read(buf); int count = 0; while (read == 0){ read = channel.read(buf); count++; if(count >3) break; } buf.flip(); while (buf.hasRemaining()){ byte[] bytes = new byte[buf.remaining()]; buf.get(bytes,0,buf.remaining()); messQue.add(bytes); } buf.clear(); key.interestOps(OP_READ|OP_WRITE); } if(key.isWritable()){ SocketChannel channel = (SocketChannel) key.channel(); ByteBuffer buf = (ByteBuffer) key.attachment(); if(messQue.size() != 0){ byte[] bytes = messQue.removeFirst(); buf.put(bytes); buf.put("\r\n".getBytes()); } buf.flip(); channel.write(buf); buf.clear(); key.interestOps(OP_READ); } } } } public static void main(String[] args) throws IOException { new Server().init(8090).accept(); } }
Client.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import static java.nio.channels.SelectionKey.*;
public class Client {
private Scanner scanner = new Scanner(System.in);
private String msg;
private Selector selector;
private SocketAddress address;
public Client init(int port) throws IOException {
address = new InetSocketAddress("localhost", port);
selector = Selector.open();
SocketChannel client = SocketChannel.open();
client.configureBlocking(false);
client.register(selector, OP_CONNECT);
client.connect(address);
input();
return this;
}
public void connect() {
try {
while (true) {
selector.select();
Set
keys = selector.selectedKeys();
Iterator
iterator = keys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); iterator.remove(); if (key.isConnectable()) { SocketChannel channel = (SocketChannel) key.channel(); if (channel.isConnectionPending()) { channel.finishConnect(); } key.attach(ByteBuffer.allocate(1024)); key.interestOps(OP_READ | OP_WRITE); System.out.println("已连接服务器"); } if (key.isReadable()) { SocketChannel channel = (SocketChannel) key.channel(); ByteBuffer buf = (ByteBuffer) key.attachment(); int read = channel.read(buf); int count = 0; while (read == 0) { read = channel.read(buf); count++; if (count > 3) break; } buf.flip(); while (buf.hasRemaining()) { byte[] bytes = new byte[buf.remaining()]; buf.get(bytes, 0, buf.remaining()); System.out.println(new String(bytes)); } buf.clear(); } if (msg != null && key.isWritable()) { SocketChannel channel = (SocketChannel) key.channel(); ByteBuffer buf = (ByteBuffer) key.attachment(); buf.put(msg.getBytes()); buf.flip(); channel.write(buf); msg = null; } } } } catch (IOException e) { e.printStackTrace(); } } private void input() { new Thread(() -> { while (true) { System.out.println("请输入"); msg = scanner.nextLine(); } }).start(); } public static void main(String[] args) throws IOException { Client client = new Client(); client.init(10000).connect(); } }
Client2.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
public class Client2 {
private Scanner scanner = new Scanner(System.in);
private String msg;
public Client2 init(int port) throws IOException {
SocketAddress address = new InetSocketAddress("localhost", port);
SocketChannel client = SocketChannel.open();
client.connect(address);
read(client);
input(client);
return this;
}
public void read(SocketChannel client) {
try {
while (true){
ByteBuffer buffer = ByteBuffer.allocate(100);
int read = client.read(buffer);
if(read > 0){
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes,0,buffer.remaining());
System.out.println(new String(bytes));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void input(SocketChannel client) {
new Thread(() -> {
while (true) {
System.out.println("请输入");
msg = scanner.nextLine();
ByteBuffer buffer = ByteBuffer.allocate(100);
try {
client.write(buffer.put(msg.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public static void main(String[] args) throws IOException {
Client2 client = new Client2();
client.init(8090);
}
}
