{"id":116180,"date":"2018-08-27T08:29:56","date_gmt":"2018-08-27T00:29:56","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=116180"},"modified":"2018-08-23T13:33:18","modified_gmt":"2018-08-23T05:33:18","slug":"java-mina-network","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-mina-network.html","title":{"rendered":"\u5982\u4f55\u642d\u5efa\u9ad8\u6027\u80fdJava\u7f51\u7edc\u6846\u67b6 MINA"},"content":{"rendered":"
<\/p>\n
Apache MINA \u662f\u4e00\u4e2a\u7f51\u7edc\u5e94\u7528\u6846\u67b6\uff0c\u6709\u52a9\u4e8e\u7528\u6237\u975e\u5e38\u65b9\u4fbf\u5730\u5f00\u53d1\u9ad8\u6027\u80fd\u3001\u9ad8\u4f38\u7f29\u6027\u7684\u7f51\u7edc\u5e94\u7528\u3002\u5b83\u901a\u8fc7Java NIO\u63d0\u4f9b\u4e86\u4e00\u4e2a\u62bd\u8c61\u7684\u3001\u4e8b\u4ef6\u9a71\u52a8\u7684\u3001\u5f02\u6b65\u7684\u4f4d\u4e8e\u5404\u79cd\u4f20\u8f93\u534f\u8bae\uff08\u5982TCP\/IP\u548cUDP\/IP\uff09\u4e4b\u4e0a\u7684API\uff0c<\/p>\n
Apache MINA \u901a\u5e38\u53ef\u88ab\u79f0\u4e4b\u4e3a\uff1a<\/p>\n
NIO \u6846\u67b6\u5e93\uff1b
\n\u5ba2\u6237\u7aef\/\u670d\u52a1\u5668\u6846\u67b6\u5e93\uff1b
\n\u6216\u8005\u4e00\u4e2a\u7f51\u7edcsocket\u5e93\u3002<\/p>\n
\u7136\u800c\uff0c\u5b83\u6240\u63d0\u4f9b\u7684\u529f\u80fd\u8fdc\u4e0d\u6b62\u8fd9\u4e9b\u3002<\/p>\n
\uff08\u4ee5\u4e0a\u5185\u5bb9\u5927\u81f4\u7ffb\u8bd1\u81eaApache MINA\u7f51\u7ad9\uff09<\/p>\n
\u5982\u5b98\u65b9\u6587\u6863\u7684\u4ecb\u7ecd\uff0cApache MINA \u662f\u4e00\u4e2a\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\uff0c\u5b83\u5bf9Java\u4e2d\u7684socket\u548cNIO\u8fdb\u884c\u4e86\u6709\u6548\u548c\u6e05\u6670\u7684\u5c01\u88c5\uff0c\u65b9\u4fbf\u5f00\u53d1\u4eba\u5458\u5f00\u53d1TCP\/UDP\u7a0b\u5e8f\uff0c\u4ece\u800c\u629b\u5f00\u5728\u4f7f\u7528\u539f\u59cb\u7684socket\u65f6\u9700\u8981\u8003\u8651\u7684\u5404\u79cd\u7e41\u6742\u800c\u53c8\u70e6\u4eba\u95ee\u9898\uff08\u7ebf\u7a0b\u3001\u6027\u80fd\u3001\u4f1a\u8bdd\u7b49\uff09\uff0c\u628a\u66f4\u591a\u7cbe\u529b\u4e13\u8457\u5728\u5e94\u7528\u4e2d\u7684\u4e1a\u52a1\u903b\u8f91\u7684\u5f00\u53d1\u4e0a\u3002<\/p>\n
Apache MINA \u6709\u4e24\u4e2a\u4e3b\u8981\u7248\u672c\uff1a2.0 \u548c 1.1\uff0c2.0\u4e0e1.1\u6709\u8f83\u5927\u7684\u533a\u522b\uff0c\u5176\u91c7\u7528java NIO\u8fdb\u884c\u5f00\u53d1\uff0c\u4f7f\u5f97\u6027\u80fd\u5f97\u5230\u6709\u6548\u7684\u63d0\u5347\uff0c\u5728\u63a5\u53e3\u65b9\u9762\u4e5f\u6709\u4e0d\u5c0f\u7684\u53d8\u5316\uff0c\u5177\u4f53\u4fe1\u606f\u53ef\u4ee5\u53c2\u89c1\u5176\u7f51\u7ad9\u8bf4\u660e\u3002<\/p>\n
\u4e0b\u9762\u7684\u4ecb\u7ecd\u4ee5 Apache MINA 2.0 \u4e3a\u4f8b\u3002<\/p>\n
\u8981\u7f16\u5199\u548c\u8fd0\u884c\u4e00\u4e2a\u57fa\u4e8eApache MINA 2.0\u7684\u7a0b\u5e8f\uff0c\u9700\u8981JDK 5.0\u4ee5\u4e0a\u7248\u672c\uff0c\u8fd8\u9700\u8981\u4ee5\u4e0b\u8f6f\u4ef6\uff1a<\/p>\n
\uff081\uff09MINA 2.x Core<\/p>\n
\u4e0b\u8f7d\u5730\u5740\uff1ahttp:\/\/mina.apache.org\/downloads.html<\/a><\/p>\n \uff082\uff09SLF4J 1.3.0 or greater<\/p>\n \u4e0b\u8f7d\u5730\u5740\uff1ahttp:\/\/www.slf4j.org\/download.html<\/a><\/p>\n \u8fd9\u91cc\u9700\u8981\u7528\u5230\u4e24\u4e2a SLF4J \u7684jar\u5305\uff1aslf4j-api.jar \u548c slf4j-jdk14.jar\u3002<\/p>\n \u5206\u522b\u89e3\u538b\u4e0b\u8f7d\u7684MINA 2.x Core \u548cSLF4J\u6587\u4ef6\uff0c\u627e\u51fa\u4e0b\u5217jar\u5305\u6587\u4ef6\uff1a<\/p>\n mina-core-2.0.0-M1.jar \u8be5\u7a0b\u5e8f\u7684\u529f\u80fd\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u5f53\u5ba2\u6237\u7aef\u8fde\u63a5\u5230\u670d\u52a1\u5668\u76849123\u7aef\u53e3\u540e\uff0c\u7a0b\u5e8f\u5c06\u670d\u52a1\u5668\u5f53\u524d\u7684\u65f6\u95f4\u4fe1\u606f\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n \u6211\u4eec\u53ef\u4ee5\u7528Eclipse\u6765\u521b\u5efa\u7f16\u5199\u8fd9\u4e2a\u7a0b\u5e8f\u3002<\/p>\n \uff081\uff09\u5728Eclipse\u4e2d\u521b\u5efa\u4e00\u4e2aJava\u9879\u76ee\uff0c\u4f8b\u5982 TimeServerProject\uff0c\u7136\u540e\u5c06mina-core-2.0.0-M1.jar\u3001slf4j-api.jar \u548c slf4j-jdk14.jar\u8fd9\u4e09\u4e2a\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u7684Libraries\u4e2d\u3002<\/p>\n \uff082\uff09\u521b\u5efa\u4e00\u4e2a MinaTimeServer \u7c7b\uff0c\u5176\u5185\u5bb9\u4e3a\uff1a<\/p>\n \uff084\uff09\u7f16\u8bd1\u5e76\u8fd0\u884cMinaTimeServer\u7c7b<\/p>\n \uff085\uff09\u6253\u5f00\u60a8\u7684\u6d4f\u89c8\u5668\uff0c\u5728\u5730\u5740\u680f\u4e2d\u8f93\u5165\u00a0http:\/\/127.0.0.1:9123\/<\/a>\uff0c\u7136\u540e\u56de\u8f66\uff0c\u4f60\u5c31\u53ef\u4ee5\u770b\u5230\u670d\u52a1\u5668\u7684\u65f6\u95f4\u663e\u793a\u5728\u60a8\u7684\u6d4f\u89c8\u5668\u5185\u4e86\u3002\u540c\u65f6\u60a8\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884cMinaTimeServer\u7684\u63a7\u5236\u53f0\u7a97\u53e3\u4e2d\u770b\u5230\u7c7b\u4f3c\u4e0b\u9762\u7684\u4fe1\u606f\uff1a<\/p>\n \u8fd9\u662f\u7a0b\u5e8f\u7684\u65e5\u5fd7\u4fe1\u606f\u4ee5\u53ca\uff0c\u6d4f\u89c8\u5668\u53d1\u9001\u8fc7\u6765\u8bf7\u6c42\u7684HTTP\u5934\u4fe1\u606f\u3002<\/p>\n \uff086\uff09\u6216\u8005\u4e5f\u53ef\u4ee5\u901a\u8fc7telnet\u8fde\u63a5\u670d\u52a1\u5668\u76849123\u7aef\u53e3\uff0c\u8fde\u6572\u4e24\u6b21\u56de\u8f66\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u670d\u52a1\u5668\u53d1\u9001\u8fc7\u6765\u7684\u65f6\u95f4\u4fe1\u606f\u4e86\u3002<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttps:\/\/www.linuxidc.com\/Linux\/2016-02\/128212.htm<\/a><\/p>\n
\nslf4j-api.jar
\nslf4j-jdk14.jar<\/p>\nimport java.io.IOException;\r\nimport java.net.InetSocketAddress;\r\nimport java.nio.charset.Charset;\r\nimport org.apache.mina.common.IdleStatus;\r\nimport org.apache.mina.common.IoAcceptor;\r\nimport org.apache.mina.filter.codec.ProtocolCodecFilter;\r\nimport org.apache.mina.filter.codec.textline.TextLineCodecFactory;\r\nimport org.apache.mina.filter.logging.LoggingFilter;\r\nimport org.apache.mina.transport.socket.nio.NioSocketAcceptor;\r\n\r\npublic class MinaTimeServer {\r\nprivate static final int PORT = 9123;\r\n\r\npublic static void main(String[] args) throws IOException {\r\nIoAcceptor acceptor = new NioSocketAcceptor();\r\n\r\nacceptor.getFilterChain().addLast(\"logger\", new LoggingFilter());\r\nacceptor.getFilterChain().addLast(\r\n\"codec\",\r\nnew ProtocolCodecFilter(new TextLineCodecFactory(Charset\r\n.forName(\"UTF-8\"))));\r\n\r\nacceptor.setHandler(new TimeServerHandler());\r\nacceptor.getSessionConfig().setReadBufferSize(2048);\r\nacceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);\r\nacceptor.bind(new InetSocketAddress(PORT));\r\n}\r\n}\r\n\r\n\uff083\uff09\u521b\u5efa\u4e00\u4e2a TimeServerHandler \u7c7b\uff0c\u5176\u5185\u5bb9\u4e3a\uff1a\r\n\r\nimport java.util.Date;\r\n\r\nimport org.apache.mina.common.IdleStatus;\r\nimport org.apache.mina.common.IoHandlerAdapter;\r\nimport org.apache.mina.common.IoSession;\r\n\r\npublic class TimeServerHandler extends IoHandlerAdapter {\r\n@Override\r\npublic void exceptionCaught(IoSession session, Throwable cause)\r\nthrows Exception {\r\ncause.printStackTrace();\r\n}\r\n\r\n@Override\r\npublic void messageReceived(IoSession session, Object message)\r\nthrows Exception {\r\nString str = message.toString();\r\nSystem.out.println(\"Message read:\");\r\nSystem.out.println(str);\r\n\r\nDate date = new Date();\r\nsession.write(date.toString());\r\nSystem.out.println(\"Message written...\");\r\nsession.close();\r\n}\r\n\r\n@Override\r\npublic void sessionIdle(IoSession session, IdleStatus status)\r\nthrows Exception {\r\nSystem.out.println(\"IDLE \" + session.getIdleCount(status));\r\n}\r\n}\r\n<\/pre>\n
\r\nMessage read:2008-4-11 12:22:44 org.apache.mina.filter.logging.LogLevel$4 log\r\n\u4fe1\u606f: CREATED\r\n2008-4-11 12:22:44 org.apache.mina.filter.logging.LogLevel$4 log\r\n\u4fe1\u606f: OPENED\r\n2008-4-11 12:22:44 org.apache.mina.filter.logging.LogLevel$4 log\r\n\u4fe1\u606f: RECEIVED: HeapBuffer[pos=0 lim=261 cap=2048: 47 45 54 20 2F 20 48 54 54 50 2F 31 2E 31 0D 0A...]\r\n\r\nGET \/ HTTP\/1.1\r\nMessage written...\r\nMessage read:\r\nAccept: *\/*\r\nMessage written...\r\nMessage read:\r\nAccept-Language: zh-cn,en-US;q=0.5\r\nMessage written...\r\nMessage read:\r\nUA-CPU: x86\r\nMessage written...\r\nMessage read:\r\nAccept-Encoding: gzip, deflate\r\nMessage written...\r\nMessage read:\r\nUser-Agent: Mozilla\/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\nMessage written...\r\nMessage read:\r\nHost: 127.0.0.1:9123\r\nMessage written...\r\nMessage read:\r\nConnection: Keep-Alive\r\nMessage written...\r\nMessage read:\r\n\r\nMessage written...\r\n2008-4-11 12:22:44 org.apache.mina.filter.logging.LogLevel$4 log\r\n\u4fe1\u606f: SENT: HeapBuffer[pos=0 lim=29 cap=30: 46 72 69 20 41 70 72 20 31 31 20 31 32 3A 32 32...]\r\n2008-4-11 12:22:44 org.apache.mina.filter.logging.LogLevel$4 log\r\n\u4fe1\u606f: SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]\r\n2008-4-11 12:22:44 org.apache.mina.filter.logging.LogLevel$4 log\r\n\u4fe1\u606f: CLOSED\r\n<\/pre>\n
\n