首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Mina入门演练例子(简单登陆验证)

2012-07-15 
Mina入门演示例子(简单登陆验证)//包含四个类,客户端两个+服务器两个//LoginClient.java,LoginClientHandl

Mina入门演示例子(简单登陆验证)

//包含四个类,客户端两个+服务器两个//LoginClient.java,LoginClientHandler.java,LoginServer.java,LoginServerHandler.java//login客户端代码package com.mina.day3;import java.net.InetSocketAddress;import java.nio.charset.Charset;import org.apache.log4j.Logger;import org.apache.mina.core.future.ConnectFuture;import org.apache.mina.core.service.IoConnector;import org.apache.mina.core.session.IoSession;import org.apache.mina.filter.codec.ProtocolCodecFilter;import org.apache.mina.filter.codec.textline.LineDelimiter;import org.apache.mina.filter.codec.textline.TextLineCodecFactory;import org.apache.mina.transport.socket.nio.NioSocketConnector;public class LoginClient {private static Logger logger = Logger.getLogger(LoginClient.class);private static String HOST = "127.0.0.1";private static int PORT = 3005;public static void main(String[] args) {IoConnector connector = new NioSocketConnector();connector.setConnectTimeoutMillis(3000);connector.getFilterChain().addLast("codec",new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));connector.setHandler(new LoginClientHandler());IoSession session = null;try {ConnectFuture future = connector.connect(new InetSocketAddress(HOST, PORT));future.awaitUninterruptibly();session = future.getSession();session.write("admin,3");} catch (Exception e) {logger.error("connecting error...", e);}session.getCloseFuture().awaitUninterruptibly();connector.dispose();}}//客户端处理器代码package com.mina.day3;import org.apache.log4j.Logger;import org.apache.mina.core.service.IoHandlerAdapter;import org.apache.mina.core.session.IoSession;public class LoginClientHandler extends IoHandlerAdapter {private static Logger logger = Logger.getLogger(LoginClientHandler.class);@Overridepublic void messageReceived(IoSession session, Object message)throws Exception { // message from serverString msgFServer = message.toString();logger.info("client received msg = " + msgFServer);}}//服务器端LoginServer.javapackage com.mina.day3;import java.net.InetSocketAddress;import java.nio.charset.Charset;import org.apache.log4j.Logger;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.textline.LineDelimiter;import org.apache.mina.filter.codec.textline.TextLineCodecFactory;import org.apache.mina.transport.socket.nio.NioSocketAcceptor;public class LoginServer {private static Logger logger = Logger.getLogger(LoginServer.class);private static int PORT = 3005;public static void main(String[] args) {IoAcceptor acceptor = null;try {acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));acceptor.getSessionConfig().setReadBufferSize(2048);acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);acceptor.setHandler(new LoginServerHandler());acceptor.bind(new InetSocketAddress(PORT));logger.info("server startup ok,port = "+PORT);} catch (Exception e) {logger.error("server startup error...", e);e.printStackTrace();}}}//服务器处理器代码package com.mina.day3;import org.apache.log4j.Logger;import org.apache.mina.core.service.IoHandlerAdapter;import org.apache.mina.core.session.IoSession;public class LoginServerHandler extends IoHandlerAdapter {public static Logger logger = Logger.getLogger(LoginServerHandler.class);@Overridepublic void messageReceived(IoSession session, Object message)throws Exception {String resultMsg = message.toString();logger.info("server received msg = "+resultMsg);if ("bye".equals(resultMsg)) { session.close(true);}//简单模拟业务逻辑处理String[] userInfo = resultMsg.split(",");String username = userInfo[0]; String password = userInfo[1];if (username.equals("admin") && password.equals("3")) {session.write("login success...");} else {session.write("login error...");}}@Overridepublic void messageSent(IoSession session, Object message) throws Exception {// longlived connectionlogger.info("message sent success...");}}
1 楼 ciding 2012-04-20   也开始玩mina呢,哈哈。
最近开发的socket也是这方面的 2 楼 独爱Java 2012-04-21   ciding 写道也开始玩mina呢,哈哈。
最近开发的socket也是这方面的
我还在入门、希望多多分享

热点排行