Use of MINA Framework

From: https://www.cnblogs.com/yanxiaoge/p/10744454.html

1. Download

1
http: //mina.apache.org/

2. Add dependent packages to the project directory (create libs (directory directory) under the project directory)

3. Add slf4j-api-1.7.26.jar and mina-core-2.0.21.jar dependent packages to the libs directory

4. To bind the two files to the project, method: select the two files, right-click, and select Add as Library

 

Code

 Server

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
 
import java.io.*;
import java.net.InetSocketAddress;
 
public  class  Server{
     public  static  void  main(String[] args) {
         //创建一个非堵塞的Server(Socker),[NIO模式]
         SocketAcceptor acceptor =  new  NioSocketAcceptor();
         //创建一个过滤器对象
         DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
 
         //设定一个过滤器,一行一行的读取数据(/r/n)
         //chain.addLast("myChain",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));//表示传数据的数据是一个对象;
         chain.addLast( "myChain" , new  ProtocolCodecFilter( new  TextLineCodecFactory()));
         //设置服务器的消息处理器
         acceptor.setHandler( new  MinaServerHandle());
         int  port = 8080;  //服务器的端口号
         try  {
             //绑定端口,并且启动服务器,立刻返回,不会堵塞
             acceptor.bind( new  InetSocketAddress(port));
 
         catch  (IOException e) {
             e.printStackTrace();
         }
         System. out .println( "MINA服务器已经开启,端口号:" +port);
     }
}
 
//服务器端的消息处理器
class  MinaServerHandle extends IoHandlerAdapter {
     //会话开始
     @Override
     public  void  sessionOpened(IoSession session) throws Exception {
         super.sessionOpened(session);
         System. out .println(session.getRemoteAddress()+ "已连接" ); //获取客户端连接的ip地址
     }
     //会话结束
     @Override
     public  void  sessionClosed(IoSession session) throws Exception {
         System. out .println( "会话结束" );
     }
 
     //接受消息
     @Override
     public  void  messageReceived(IoSession session, Object message) throws Exception {
         super.messageReceived(session, message);
         System. out .println(message);  //接受消息
         session.write( "hellow" );
     }
}

 

  

 

Client 1

1. You can use the telnet tool that comes with window to connect to the server and communicate

First open Telnet, in the control panel, open programs and functions, in open (open or close the windows function), select the Telnet client, OK

Test: Enter telnet localhost (server IP address) 8080 (server port) on the console, then

Client 2

2. Write your own client program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
 
import java.net.InetSocketAddress;
import java.util.Scanner;
 
public  class  client {
     public  static  void  main(String[] args) {
         NioSocketConnector conn =  new  NioSocketConnector();
         DefaultIoFilterChainBuilder chain = conn.getFilterChain();
         chain.addLast( "myChain" , new  ProtocolCodecFilter( new  TextLineCodecFactory()));
         conn.setHandler( new  MinaClientHandle());
         conn.setConnectTimeoutCheckInterval(3000);
 
         //连接服务器
         ConnectFuture cf = conn.connect( new  InetSocketAddress( "localhost" , 8080));
         cf.awaitUninterruptibly();  //等待连接成功;
         Scanner input =  new  Scanner(System. in );
         while  ( true ){
             System. out .println( "输入消息" );
             String msg = input.nextLine();
             cf.getSession().write(msg);
         }
         //等待服务器关闭连接
         //cf.getSession().getCloseFuture().awaitUninterruptibly();
         //conn.dispose();
     }
}
class  MinaClientHandle extends IoHandlerAdapter {
     @Override
     public  void  sessionOpened(IoSession session) throws Exception {
         super.sessionOpened(session);
         System. out .println( "已连接" );
     }
     @Override
     public  void  sessionClosed(IoSession session) throws Exception {
         super.sessionClosed(session);
         System. out .println( "已关闭" );
     }
     @Override
     public  void  messageReceived(IoSession session, Object message) throws Exception {
         super.messageReceived(session, message);
         System. out .println( "服务器返回消息:" +message);
     }
}

  

Guess you like

Origin www.cnblogs.com/sharpest/p/12702772.html