转自:https://www.cnblogs.com/yanxiaoge/p/10744454.html

1、下载

1
http://mina.apache.org/

2、将依赖包添加到工程目录下(在工程目录下创建libs(directory目录))

3、将 slf4j-api-1.7.26.jar 和 mina-core-2.0.21.jar 依赖包添加到libs目录下

4、给这两个文件和工程绑定关系,方法:选中两个文件,右击,选择 Add as Library

 

代码实现

 服务端

 

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");
    }
}

 

  

 

客户端1

1.可以使用window自带的telnet工具和服务器连接,并进行通信

首先打开Telnet,在控制面板中,打开程序和功能,在打开 (打开或关闭windows 功能),选中Telnet 客户端 ,确定

测试:控制台上输入  telnet localhost(服务器的ip地址)   8080(服务器的端口) ,即可

客户端2

2.自己写客户端程序

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);
    }
}

  

posted on 2020-04-15 03:39  Sharpest  阅读(344)  评论(0编辑  收藏  举报