使用Mina传输Java对象

openkk 10年前

This is apache-mina-2.0.4, please drink code....

 

下面是所要传输的实体类UserInfo.java

package com.mina.model;    import java.io.Serializable;    /**   * @see Mina传输的实体类,要求其实现Serializable接口   */  @SuppressWarnings("serial")  public class UserInfo implements Serializable{   private String name;      public UserInfo(String name){    this.name = name;   }      public String getName() {    return name;   }  }

下面是Mina编写的服务端主类MyServer.java</strong></span>

package com.mina.server;    import java.io.IOException;  import java.net.InetSocketAddress;    import org.apache.mina.core.service.IoAcceptor;  import org.apache.mina.core.session.IdleStatus;  import org.apache.mina.filter.codec.ProtocolCodecFilter;  import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;    public class MyServer {   public static void main(String[] args) throws IOException {    int bindPort = 9876;        IoAcceptor acceptor = new NioSocketAcceptor();        acceptor.getSessionConfig().setReadBufferSize(2048);    acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);        //设定服务器解析消息的规则是以Object对象为单位进行传输,注意此时该对象需实现Serializable接口    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));        acceptor.setHandler(new ServerHandler());        acceptor.bind(new InetSocketAddress(bindPort));        System.out.println("MinaServer is startup, and it`s listing on := " + bindPort);   }  }

下面是服务端的消息处理器ServerHandler.java</strong></span>

package com.mina.server;    import org.apache.mina.core.service.IoHandlerAdapter;  import org.apache.mina.core.session.IoSession;    import com.mina.model.UserInfo;    public class ServerHandler extends IoHandlerAdapter {   @Override   public void messageReceived(IoSession session, Object message) throws Exception {    UserInfo ui = (UserInfo)message; //我们已设定了服务器解析消息的规则是以UserInfo对象为单位进行传输    System.out.println("收到客户机发来的用户名:" + ui.getName());    session.write(new UserInfo(ui.getName() + "==>>是个神秘的人"));   }      @Override   public void sessionOpened(IoSession session) throws Exception{    System.out.println("InComing Client:" + session.getRemoteAddress());   }  }

接下来是Mina编写的客户端主类MyClient.java</strong></span>

package com.mina.client;    import java.net.InetSocketAddress;    import org.apache.mina.core.service.IoConnector;  import org.apache.mina.filter.codec.ProtocolCodecFilter;  import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;  import org.apache.mina.transport.socket.nio.NioSocketConnector;    import com.mina.model.UserInfo;    public class MyClient {   public static void main(String[] args) {    IoConnector connector = new NioSocketConnector();        connector.setConnectTimeoutMillis(30000);        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));        connector.setHandler(new ClientHandler(new UserInfo("张起灵")));        connector.connect(new InetSocketAddress("127.0.0.1", 9876));   }  }

最后是客户端的消息处理器ClientHandler.java</strong></span>

package com.mina.client;    import org.apache.mina.core.service.IoHandlerAdapter;  import org.apache.mina.core.session.IoSession;    import com.mina.model.UserInfo;    public class ClientHandler extends IoHandlerAdapter {   private final UserInfo ui;      public ClientHandler(UserInfo ui){    this.ui = ui;   }     @Override   public void sessionOpened(IoSession session) throws Exception {    session.write(ui);   }      @Override   public void messageReceived(IoSession session, Object message) throws Exception {    UserInfo ui = (UserInfo)message;    System.out.println("收到服务机发来的用户名:" + ui.getName());   }     @Override   public void exceptionCaught(IoSession session, Throwable cause) throws Exception {    System.out.println("与" + session.getRemoteAddress() + "通信过程中出现错误:[" + cause.getMessage() + "]..连接即将关闭....");    session.close(false);    session.getService().dispose();   }  }
转自:http://blog.csdn.net/jadyer/article/details/7662878