package com.duing.chat;
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.Set;
public class ChatServer2 {
private ServerSocketChannel socketChannel;
private Selector selector;
private ChatServer2() throws IOException {
socketChannel=ServerSocketChannel.open();
selector=Selector.open();
SocketAddress socketAddress=new InetSocketAddress(6666);
socketChannel.socket().bind(socketAddress);
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_ACCEPT);
}
public void listenClient () throws IOException {
System.out.println("服务端启动了......");
while (true){
int select = selector.select();
if (select>0){
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()){
SelectionKey key = iterator.next();
iterator.remove();
if (key.isAcceptable()){
SocketChannel accept = socketChannel.accept();
accept.configureBlocking(false);
System.out.println("用户:"+accept.socket().getRemoteSocketAddress()+"上线了");
accept.register(selector,SelectionKey.OP_READ);
continue;
}
if (key.isReadable()){
this.ReadData(key);
}
}
}
}
}
public void ReadData(SelectionKey key){
SocketChannel socketChannel=null;
try {
socketChannel=(SocketChannel) key.channel();
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
socketChannel.read(byteBuffer);
byteBuffer.flip();
String s=new String(byteBuffer.array());
System.out.println(socketChannel.socket().getRemoteSocketAddress()+"用户mag:"+s);
sendToOther(s,socketChannel);
}catch (Exception e){
System.out.println("用户:"+socketChannel.socket().getRemoteSocketAddress()+"下线");
key.cancel();
socketChannel.socket();
}
}
public void sendToOther(String mag,SocketChannel socketChannel) throws IOException {
Set<SelectionKey> keys = selector.keys();
for (SelectionKey key : keys) {
Channel channel=key.channel();
if (channel instanceof SocketChannel && channel!=socketChannel){
SocketChannel socketChannel1=(SocketChannel) channel;
ByteBuffer byteBuffer=ByteBuffer.wrap(mag.getBytes());
socketChannel1.write(byteBuffer);
byteBuffer.flip();
}
}
}
public static void main(String[] args) {
try {
ChatServer2 server2=new ChatServer2();
server2.listenClient();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.duing.chat;
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.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class ChatClient2 {
private SocketChannel socketChannel;
private Selector selector;
private ChatClient2() throws IOException {
socketChannel=SocketChannel.open();
SocketAddress socketAddress=new InetSocketAddress("127.0.0.1",6666);
socketChannel=socketChannel.open(socketAddress);
selector=Selector.open();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
System.out.println("用户:"+socketChannel.getLocalAddress()+"上线");
}
public void sendate(String mag) throws IOException {
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
byteBuffer.put(mag.getBytes());
byteBuffer.flip();
socketChannel.write(byteBuffer);
}
public void readData() throws IOException {
int select = selector.select();
if (select>0){
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()){
SelectionKey next = iterator.next();
if (next.isReadable()){
SocketChannel socketChannel=(SocketChannel) next.channel();
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
socketChannel.read(byteBuffer);
byteBuffer.flip();
String s=new String(byteBuffer.array());
System.out.println(socketChannel.socket().getRemoteSocketAddress()+"发送信息:"+s);
}
}
}
}
public static void main(String[] args) throws IOException {
final ChatClient2 chatClient2=new ChatClient2();
new Thread(){
@Override
public void run() {
try {
chatClient2.readData();
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
Scanner scanner=new Scanner(System.in);
while (scanner.hasNextLine()){
chatClient2.sendate(scanner.nextLine());
}
}
}