면책 조항 :이 문서는 블로거 원본입니다, 추적 에 의해-SA의 CC 4.0 저작권 계약, 복제, 원본 소스 링크이 문을 첨부 해주세요.
서버
package com.zyc.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
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.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
/**
* @author zhuyc
* @create 2019-03-27 20:49
*/
public class NioServer {
public static void main(String[] args) throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
ServerSocket serverSocket = serverSocketChannel.socket();
serverSocket.bind(new InetSocketAddress(8899));
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
Map<String,SocketChannel> clientMap = new HashMap<>();
while (true){
try {
selector.select();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
selectionKeys.forEach(selectionKey -> {
SocketChannel client;
try {
if(selectionKey.isAcceptable()){
ServerSocketChannel server = (ServerSocketChannel) selectionKey.channel();
client = server.accept();
client.configureBlocking(false);
client.register(selector,SelectionKey.OP_READ);
String key = "["+ UUID.randomUUID().toString() +"]";
clientMap.put(key,client);
}else if (selectionKey.isReadable()){
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
client = (SocketChannel) selectionKey.channel();
int count = client.read(byteBuffer);
if(count > 0){
byteBuffer.flip();
Charset charset = Charset.forName("UTF-8");
String receiveMsg = String.valueOf(charset.decode(byteBuffer).array());
System.out.println(client +" : "+receiveMsg);
String clinetKey = null;
for(Map.Entry<String,SocketChannel> entry:clientMap.entrySet()){
if(client == entry.getValue()){
clinetKey = entry.getKey();
break;
}
}
for(Map.Entry<String,SocketChannel> entry:clientMap.entrySet()){
SocketChannel sc = entry.getValue();
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
writeBuffer.put((clinetKey +": "+receiveMsg).getBytes());
writeBuffer.flip();
sc.write(writeBuffer);
}
}
// while(count > 0){
// System.out.println(count);
// byteBuffer.clear();
// count = client.read(byteBuffer);
//
//
//
//
// }
System.out.println("end reading");
}
}catch (Exception ex){
ex.printStackTrace();
}
});
selectionKeys.clear();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
주의 사항
1. 어드레스 서버는 여전히 구속된다 ServerSocket
생성 소켓 SockertChannel () 메소드 ServerSocket를,
처리 2.SelectionKey의 컬렉션을 후 需要清空
. 여기에 달성하기 위해 명확한 방법이다. 또한 탐색하는 동안 처리, 명확 할 수있다
두 클라이언트
package com.zyc.nio;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.time.LocalDateTime;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author zhuyc
* @create 2019-03-31 9:10
*/
public class NioClient {
public static void main(String[] args) {
SocketChannel socketChannel = null;
try {
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
Socket socket = socketChannel.socket();
Selector selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_CONNECT);
socketChannel.connect(new InetSocketAddress("127.0.0.1",8899));
while (true){
selector.select();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
for (SelectionKey selectionKey:selectionKeys){
if(selectionKey.isConnectable()){
SocketChannel client = (SocketChannel)selectionKey.channel();
if(client.isConnectionPending()){
client.finishConnect();
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
writeBuffer.put((LocalDateTime.now()+" connect success").getBytes());
writeBuffer.flip();
client.write(writeBuffer);
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(new Runnable() {
@Override
public void run() {
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
try {
String content = bufferedReader.readLine();
writeBuffer.clear();
writeBuffer.put(content.getBytes());
writeBuffer.flip();
client.write(writeBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
});
client.register(selector,SelectionKey.OP_READ);
}
}else if(selectionKey.isReadable()){
SocketChannel sc = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int count = sc.read(byteBuffer);
if(count > 0){
String receiveMsg = new String(byteBuffer.array(),0,count);
System.out.println(receiveMsg);
}
}
}
selectionKeys.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
주의 사항
- 클라이언트 액세스 어드레스 SocketChannel에 직접 연결 방법을 통해 달성된다
//;SocketChannel支持在非阻塞模式下连接:connect方法发起远程TCP连接,但是其立即返回,
//不过连接的过程仍然在进行,之后需要通过finishConnect方法检测并完成链接,可以通过isConnectionPending()检测链接是否正在进行.
if(client.isConnectionPending()){
client.finishConnect();
...
소스 해설
/**
* Tells whether or not a connection operation is in progress on this
* channel.
*
* @return <tt>true</tt> if, and only if, a connection operation has been
* initiated on this channel but not yet completed by invoking the
* {@link #finishConnect finishConnect} method
*/
public abstract boolean isConnectionPending();
- 클라이언트는 (서버가 메시지를 전송) 입력 스트림을 기다리는 또한 우리가 서버로 전송되어 사용자 입력을 기다릴 필요가있다 할 필요가있다. 그래서 두 개의 스레드, 스레드는 입력 성공은 채널을 작성합니다 일단, 콘솔 입력을 쓰기 위해 대기하고있다.