- 浏览: 295769 次
- 性别:
- 来自: 合肥
文章分类
最新评论
-
Xiaoanemy:
我怎么就是不行Error opening zip file o ...
javarebel不用再反复重启tomcat -
fly_hyp:
lvwenwen 写道相对hessian来说有其他什么优势?应 ...
一个很牛的架构组件(Dubbo) -
lvwenwen:
相对hessian来说有其他什么优势?
一个很牛的架构组件(Dubbo) -
dj4307665:
想了解下,相对hessian来说有其他什么优势?
一个很牛的架构组件(Dubbo) -
fly_hyp:
sweat89 写道怎么解决的啊?忘了。怎么说呢?自己写的代码 ...
Spring之恶心错误记录
我用mina写了一个Memcache服务端的通讯模拟程序
使用的是 mina-core-2.0.0-M1.jar
一共3各类
HypTestServerHandler
MinaUtilRun 启动类
TimeServerHandler
欢迎和我交流着方面的问题。
就是下面这些代码吧!
使用的是 mina-core-2.0.0-M1.jar
一共3各类
HypTestServerHandler
MinaUtilRun 启动类
TimeServerHandler
欢迎和我交流着方面的问题。
public class TimeServerHandler extends IoHandlerAdapter { public void exceptionCaught(IoSession session, Throwable t) throws Exception { // 出现异常的时候调用. t.printStackTrace(); session.close(); } public void messageReceived(IoSession session, Object msg) throws Exception { // 接收客户端新的消息的时候调用. String str = msg.toString(); if (str.trim().equalsIgnoreCase("quit")) { session.close(); return; } Date date = new Date(); session.write(date.toString()); System.out.println("Message written..."); } public void sessionCreated(IoSession session) throws Exception { // 当一个客户端连接到服务器的时候被调用. System.out.println("Session created..."); if (session.getTransportType() == TransportType.SOCKET) ((SocketSessionConfig) session.getConfig()) .setReceiveBufferSize(2048); session.setIdleTime(IdleStatus.BOTH_IDLE, 10); } }
public class MinaUtilRun { private static final int PORT = 11211; public static void test() throws Exception { ByteBuffer.setUseDirectBuffers(false); ByteBuffer.setAllocator(new SimpleByteBufferAllocator()); IoAcceptor acceptor = new SocketAcceptor(); SocketAcceptorConfig cfg = new SocketAcceptorConfig(); //创建一个与SocketAcceptor相关联的配置对象. cfg.getSessionConfig().setReuseAddress( true ); cfg.getFilterChain().addLast( "logger", new LoggingFilter() ); cfg.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new HypLineCodecFactory( Charset.forName( "GBK" )))); //acceptor.bind( new InetSocketAddress(PORT), new TimeServerHandler(), cfg);//绑定端口,处理对象和配置对象. acceptor.bind( new InetSocketAddress(PORT), new HypTestServerHandler(), cfg);//绑定端口,处理对象和配置对象. System.out.println("MINA Time server started."); } /** * @param args */ public static void main(String[] args) { try { test(); } catch (Exception e) { e.printStackTrace(); } } }
public class HypTestServerHandler extends IoHandlerAdapter { public void exceptionCaught(IoSession session, Throwable t) throws Exception { // 出现异常的时候调用. t.printStackTrace(); session.close(); } public void messageReceived(IoSession session, Object msg) throws Exception { // 接收客户端新的消息的时候调用. String stat = (String) session.getAttribute("stat"); String str = msg.toString(); JavaUtil.debugPrint("receive:" + str); if(stat.equals("GetCommand")){ if(str.equals("version")){ session.write("VERSION 1.2.2"); JavaUtil.debugPrint("response verison .... VERSION 1.2.2"); return; } if(str.startsWith("set ")){ String[] parts = str.split(" "); int dataSize = Integer.parseInt(parts[4]); JavaUtil.debugPrint("dataSize:" + dataSize); session.setAttribute("dataSize", dataSize); session.setAttribute("readSize", 0); session.setAttribute("stat", "readData"); session.setAttribute("dataBuf", new StringBuilder()); session.setAttribute("currentCmd", parts); } }else if(stat.equals("readData")){ int dataSize = (Integer)session.getAttribute("dataSize"); int readSize = (Integer)session.getAttribute("readSize"); StringBuilder buf = (StringBuilder)session.getAttribute("dataBuf"); if(buf.length() > 0){ buf.append("\r\n"); } buf.append(str); readSize += str.length() + 2; session.setAttribute("readSize", readSize); JavaUtil.debugPrint("read data:" + readSize + "/" +dataSize ); if(readSize == dataSize){ //处理命令 String[] parts = (String[])session.getAttribute("currentCmd"); System.out.println("buf.length():" + buf.length()); System.out.println(buf.toString()); session.write("STORED"); session.setAttribute("stat", "GetCommand"); } } //Date date = new Date(); //session.write(date.toString()); System.out.println("Message written..."); } public void sessionCreated(IoSession session) throws Exception { // 当一个客户端连接到服务器的时候被调用. System.out.println("Session created..."); if (session.getTransportType() == TransportType.SOCKET) ((SocketSessionConfig) session.getConfig()) .setReceiveBufferSize(2048); session.setIdleTime(IdleStatus.BOTH_IDLE, 10); session.setAttribute("stat", "GetCommand"); } }
评论
2 楼
fly_hyp
2010-01-03
xianglei 写道
new HypLineCodecFactory( Charset.forName( "GBK" ))) HypLineCodecFactory这个类呢?
就是下面这些代码吧!
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */ //这是从Mina的源代码摘过来的 package cn.sh.flyhyp.memcache.client.codec; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; /** * A delimiter which is appended to the end of a text line, such as * <tt>CR/LF</tt>. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 714210 $, $Date: 2009-05-06 06:47:16 $ */ public class LineDelimiter { /** * the line delimiter constant of the current O/S. */ public static final LineDelimiter DEFAULT; static { ByteArrayOutputStream bout = new ByteArrayOutputStream(); PrintWriter out = new PrintWriter(bout); out.println(); DEFAULT = new LineDelimiter(new String(bout.toByteArray())); } /** * A special line delimiter which is used for auto-detection of * EOL in {@link MemcachedDecoder}. If this delimiter is used, * {@link MemcachedDecoder} will consider both <tt>'\r'</tt> and * <tt>'\n'</tt> as a delimiter. */ public static final LineDelimiter AUTO = new LineDelimiter(""); /** * The CRLF line delimiter constant (<tt>"\r\n"</tt>) */ public static final LineDelimiter CRLF = new LineDelimiter("\r\n"); /** * The line delimiter constant of UNIX (<tt>"\n"</tt>) */ public static final LineDelimiter UNIX = new LineDelimiter("\n"); /** * The line delimiter constant of MS Windows/DOS (<tt>"\r\n"</tt>) */ public static final LineDelimiter WINDOWS = CRLF; /** * The line delimiter constant of Mac OS (<tt>"\r"</tt>) */ public static final LineDelimiter MAC = new LineDelimiter("\r"); /** * The line delimiter constant for NUL-terminated text protocols * such as Flash XML socket (<tt>"\0"</tt>) */ public static final LineDelimiter NUL = new LineDelimiter("\0"); private final String value; /** * Creates a new line delimiter with the specified <tt>value</tt>. */ public LineDelimiter(String value) { if (value == null) { throw new NullPointerException("delimiter"); } this.value = value; } /** * Return the delimiter string. */ public String getValue() { return value; } @Override public int hashCode() { return value.hashCode(); } @Override public boolean equals(Object o) { if (!(o instanceof LineDelimiter)) { return false; } LineDelimiter that = (LineDelimiter) o; return this.value.equals(that.value); } @Override public String toString() { StringBuffer buf = new StringBuffer(); buf.append("delimiter:"); if (value.length() == 0) { buf.append(" auto"); } else { for (int i = 0; i < value.length(); i++) { buf.append(" 0x"); buf.append(Integer.toHexString(value.charAt(i))); } } return buf.toString(); } }
1 楼
xianglei
2010-01-03
new HypLineCodecFactory( Charset.forName( "GBK" ))) HypLineCodecFactory这个类呢?
发表评论
-
为什么中文编程项目失败率特别高?
2017-06-11 10:58 357不少中文编程语言都是创造者一时热情。觉得发明很伟大,想当然的 ... -
阿里大数据架构
2014-06-26 10:16 719阿里大数据架构 -
JDI Java程序员的高级玩具
2013-12-30 10:03 1377JDI,Java程序员的高级玩 ... -
异构云操作系统需要具备的18总特性和3类支持
2013-12-24 14:07 1042什么是云操作系统? 相对于单机操作系统,网络操作系统。至少 ... -
安全云存储系统技术总结
2013-12-05 16:15 935阅读了一下《计算机研究与发展-安全云存储系统与关键技 ... -
大型团队Java项目日志自由激活的设计
2013-11-25 13:40 799大型团队Java项目日志自由激活的设计 摘要: ... -
可以用 JavaScript 来写的应用,最终都会用 JavaScript 来写
2013-11-05 17:04 782这句话我喜欢。 凡是可以用 JavaScrip ... -
评价挺高的Java Web Profile 开源工具 Jwebap
2013-11-04 09:19 700想法很不错,作者也很努力,评价也不错。不过从历史的角 ... -
playframework 非常不错的web开发框架
2013-09-18 15:34 793playframework 非常不错的web开发 ... -
knockoutjs 非常不错的js mvc 框架
2013-09-18 15:32 625knockoutjs 非常不错的js mvc 框架 ... -
一个企业级的自动化工具gradle
2013-09-02 16:08 762还没有用过,理念不错,我喜欢。值得研究一下。 ... -
activeJDBC现在很流行的一个JDBC应用
2013-09-02 16:05 833activeJDBC现在很流行的一个JDBC应用 ... -
如何解决 emma 在 JDK7 下 java.lang.ClassFormatError 错误
2013-09-02 16:04 715JVM 启动时可以加入 “-XX:-UseSplit ... -
10 个非常重要的 HotSpot JVM 参数
2013-08-17 20:42 7871) 跟 Java 堆大小相关的 JVM 内存参数 下 ... -
A JSP to print all the stacks
2013-06-19 17:29 795A JSP to print all the stacks ... -
Ubuntu11.10下解决 jmap等jdk工具attach pid错误
2013-05-13 18:05 7931.错误案例 java] view plaincop ... -
对Emma的使用进行一些总结
2013-01-06 17:47 970对Emma的使用进行一些总结 写道 首先将 ... -
一个很牛的架构组件(Dubbo)
2012-12-27 17:52 2617这是一个在阿里内部广泛使用的,管理SOA组件间互相调用的基本框 ... -
Open Id and Open Auth
2011-05-31 10:00 1393Open Id and Open Auth 1.a ... -
redis 初学指导
2010-06-26 23:08 1293如果你初学或者对redis很有兴趣下面的文字可能会对你有帮助 ...
相关推荐
我自己写的使用mina框架实现cmpp2.0服务端,经过一段使用解决了几个bug现在比较稳定。
Mina 服务端客户端示例程序
mina 长连接 客户端+服务端 实现长连接可以收发消息正确部署可以使用需要的朋友可以下载看看
Apache Mina Server 是一个网络通信应用框架,本项目是Mina开发的 服务端、客户端,例子,maven构建
MINA 服务端和客户端demo
这是一个基于spring mina 的服务端整合框架
mina2推送demo服务端推送结束,正式可靠,验证过,可以使用
里面包含了mina服务端和客户端,我的测试用的是,运行demo里面的demo1server.java启动服务端的虚拟机,然后安装mina下的客户端到手机上就能实现通信。端口应该是对应好的。
mina server开发服务端/客户端代码,供大家一起共同分享学习。
一个对推送 及时的处理结束,长连接保持,以及对客户端 和服务端的实现,
本源码是《NIO框架入门(二):服务端基于MINA2的UDP双向通信Demo演示》一文的MINA2服务端源码实现,详见:http://www.52im.net/thread-373-1-1.html
mina2服务端客户端实例,直接导入eclipse项目即可。
一个源代码学习服务端 ,基于mina 。控制单位可以加载csv和读取xml
说明:该服务器框架试用于需要频繁修改数据库内容的游戏,没有使用数据库缓存,jdbc直接修改数据库内容。 申明: 我是个新手,有出错的地方莫怪哈,把源码发布出来更多的是想大伙帮忙改进下框架,改进的代码和新...
mina mmorpg服务端源码 采用amf3编解码格式
NULL 博文链接:https://aniyo.iteye.com/blog/1609337
mina框架中socket使用,有服务端和客户端。这是比较完整的项目。希望能给后人点参考
mina框架实现tcp长连接服务端所需要的jar包
minafileserver(Socket客户端+Mina服务端传文件)
mina服务端工程例子包括相关源码,实现了socket 的nio