public class ServerDemo1 {
public static final int PORT= 10085 ;
public Selector selector;
public ServerSocketChannel serverSocketChannel;
public ByteBuffer sendBuffer;
public ByteBuffer recBuffer;
public ServerDemo1 ( ) {
sendBuffer= ByteBuffer. allocate ( 1024 ) ;
recBuffer= ByteBuffer. allocate ( 1024 ) ;
try {
this . selector= Selector. open ( ) ;
this . serverSocketChannel= ServerSocketChannel. open ( ) ;
this . serverSocketChannel. bind ( new InetSocketAddress ( PORT) ) ;
this . serverSocketChannel. configureBlocking ( false ) ;
this . serverSocketChannel. register ( selector, SelectionKey. OP_ACCEPT) ;
System. out. println ( "server success init" ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
private void listen ( ) throws IOException {
while ( true ) {
int select = selector. select ( ) ;
if ( select > 0 ) {
Set< SelectionKey> selectionKeys = selector. selectedKeys ( ) ;
if ( selectionKeys. isEmpty ( ) ) {
continue ;
}
Iterator< SelectionKey> iterator = selectionKeys. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
SelectionKey nextkey = iterator. next ( ) ;
handle ( nextkey) ;
iterator. remove ( ) ;
}
}
}
}
private void handle ( SelectionKey nextkey) {
SocketChannel client= null;
try {
if ( nextkey. isValid ( ) && nextkey. isAcceptable ( ) ) {
client = serverSocketChannel. accept ( ) ;
client. configureBlocking ( false ) ;
client. register ( selector, SelectionKey. OP_READ) ;
} else if ( nextkey. isReadable ( ) ) {
client = ( SocketChannel) nextkey. channel ( ) ;
recBuffer. clear ( ) ;
int read = client. read ( recBuffer) ;
if ( read > 0 ) {
String message = new String ( recBuffer. array ( ) , 0 , read) ;
System. out. println ( "server收到客户端信息现在转发" + client. getRemoteAddress ( ) + message) ;
sendToOtherclient ( message, client ) ;
}
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
private void sendToOtherclient ( String message, SocketChannel selfclient) throws IOException {
Channel targetclient;
for ( SelectionKey selectkey: selector. keys ( ) ) {
targetclient= selectkey. channel ( ) ;
if ( targetclient instanceof SocketChannel && targetclient != selfclient && targetclient != serverSocketChannel) {
SocketChannel desc= ( SocketChannel) targetclient;
sendBuffer. clear ( ) ;
sendBuffer. put ( message. getBytes ( ) ) ;
sendBuffer. flip ( ) ;
desc. write ( sendBuffer) ;
}
}
}
public static void main ( String[ ] args) throws IOException {
ServerDemo1 serverDemo1 = new ServerDemo1 ( ) ;
serverDemo1. listen ( ) ;
}
}
public class ClientDemo1 {
private static final int PORT= 10085 ;
private static final String IP= "127.0.0.1" ;
private SocketChannel socketChannel;
private ByteBuffer sendBuffer;
private ByteBuffer recBuffer;
public ClientDemo1 ( ) {
this . recBuffer= ByteBuffer. allocate ( 1024 ) ;
this . sendBuffer= ByteBuffer. allocate ( 1024 ) ;
try {
socketChannel= SocketChannel. open ( ) ;
socketChannel. configureBlocking ( false ) ;
socketChannel. connect ( new InetSocketAddress ( IP, PORT) ) ;
System. out. println ( socketChannel. getRemoteAddress ( ) + "客户端初始化完毕" ) ;
connectServer ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
private void connectServer ( ) throws IOException {
System. out. println ( "客户端开始连接" ) ;
if ( socketChannel. isConnectionPending ( ) ) {
socketChannel. finishConnect ( ) ;
sendMessage ( socketChannel. getRemoteAddress ( ) + "用户上线!" ) ;
}
}
public void sendMessage ( String message) throws IOException {
sendBuffer. clear ( ) ;
sendBuffer. put ( message. getBytes ( ) ) ;
sendBuffer. flip ( ) ;
socketChannel. write ( sendBuffer) ;
}
public void recMessage ( ) throws IOException {
recBuffer. clear ( ) ;
int read = socketChannel. read ( recBuffer) ;
if ( read > 0 ) {
String message= new String ( recBuffer. array ( ) , 0 , read) ;
System. out. println ( "来自其他客户端的消息--" + message) ;
}
}
public static void main ( String[ ] args) throws IOException {
ClientDemo1 clientDemo1 = new ClientDemo1 ( ) ;
new Thread ( ( ) - > {
while ( true ) {
try {
clientDemo1. recMessage ( ) ;
Thread. sleep ( 2000 ) ;
} catch ( IOException | InterruptedException e) {
e. printStackTrace ( ) ;
}
}
} ) . start ( ) ;
Scanner scanner= new Scanner ( System. in) ;
System. out. println ( "输入你想要发送的信息" ) ;
while ( scanner. hasNext ( ) ) {
System. out. println ( "输入你想要发送的信息" ) ;
String message= scanner. nextLine ( ) ;
try {
clientDemo1. sendMessage ( message) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}