`
simple1024
  • 浏览: 74218 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Mina入门

    博客分类:
  • Mina
阅读更多

服务器---客户端互相响应

 

简单的小例子,理解MINA是怎么工作的。

 

需要导入的包:

mina-core-2.00-M1.jar;

log4j-over-slf4j-1.5.0.jar;

slf4j-nop-1.5.0.jar

 

 

MINA服务器端代码

 

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;


//负责处理连接上来的客户机,即消息处理器
public class MinaServerHandler extends IoHandlerAdapter{

  //客户端发送的消息到达时
  @Override
  public void messageReceived(IoSession session, Object message) throws Exception {
    // TODO Auto-generated method stub
    String s = (String)message;
    System.out.println("来自客户端的消息:"+s);
    session.write(s);
  }
  
  //一个客户端关闭时
  @Override
  public void sessionClosed(IoSession session) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("one Client Disconnect");
  }
  
  //一个客户端接入时
  @Override
  public void sessionCreated(IoSession session) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("one Client Connection");
  }
  
}

 

启动服务器代码

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
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;



public class StartServer {
  public static void main(String[] args) {
    //创建一个非阻塞的serever端socket,用Nio
    SocketAcceptor acceptor = new NioSocketAcceptor();
    //创建接收数据的过滤器
    DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
    //设定这个过滤器规则(将一行一行读取数据)
    chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory()));
    //设定服务器端的消息处理器:一个MinaServerHandler对象
    acceptor.setHandler(new MinaServerHandler());
    //服务器端绑定的端口
    int bindPort = 10000;
    //绑定端口,启动服务器
    try {
      acceptor.bind(new InetSocketAddress(bindPort));
    } catch(IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    System.out.println("Mina server is listing on:="+bindPort);
  }
}

 

MINA客户端代码

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;


//Mina客户端消息处理器类
public class MinaClientHandler extends IoHandlerAdapter  {

  @Override
  public void messageReceived(IoSession session, Object message) throws Exception {
    // TODO Auto-generated method stub
    String s = (String)message;
    System.out.println("来自服务器的消息:"+s);
    session.write(s);
  }

  @Override
  public void sessionClosed(IoSession session) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("服务器走了~");
  }

  @Override
  public void sessionCreated(IoSession session) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("one Client Connection"+session.getRemoteAddress());
    session.write("我来了······");
  }
  
}

 

启动客户端代码

import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;



public class StartClient {
  @SuppressWarnings("deprecation")
  public static void main(String[] args) {
    //create tcp/ip connector
    NioSocketConnector connector = new NioSocketConnector();
    //创建接收数据的过滤器
    DefaultIoFilterChainBuilder chain = connector.getFilterChain();
    //设定这个过滤器将一行一行读数据
    chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory()));
    connector.setHandler(new MinaClientHandler());
    connector.setConnectTimeout(30);
    //连接到服务器
    ConnectFuture cf = connector.connect(new InetSocketAddress("localhost",10000));
    //wait for the connection attempt to be finished
    cf.awaitUninterruptibly();
    cf.getSession().getCloseFuture().awaitUninterruptibly();
    connector.dispose();
  }
}

 

先启动服务器,再启动客户端,就ok了,你的电脑可以狂奔了。

 

不写客户端可用

cmd-->telnet localhost 10000

来对话

 

 

1
0
分享到:
评论
3 楼 u010552708 2013-09-12  
slf4j老报错,最后直接在浏览器里测的
2 楼 stevenjohn 2012-07-14  
tywo45 写道
您好!我刚刚接触mina,把你的程序跑起来后,不停的出现
引用
来自客户端的消息:我来了······
来自客户端的消息:我来了······
来自客户端的消息:我来了······
来自客户端的消息:我来了······
来自客户端的消息:我来了······
来自客户端的消息:我来了······
......

在哪里有循环了?




是啊,那里有循环呢,怎么循环个没完没了的?
1 楼 tywo45 2011-11-13  
您好!我刚刚接触mina,把你的程序跑起来后,不停的出现
引用
来自客户端的消息:我来了······
来自客户端的消息:我来了······
来自客户端的消息:我来了······
来自客户端的消息:我来了······
来自客户端的消息:我来了······
来自客户端的消息:我来了······
......

在哪里有循环了?

相关推荐

Global site tag (gtag.js) - Google Analytics