配件综合 2011-08-08 17:20
浏览 208
已采纳

MINA的行号计数

我按照网上的例子做了一个简单的MINA回显的服务器,现在我想加一个功能,即在客户与服务器交互时为每句话加上他是第几句说的话,现在加得只能是不断地累加,比如俩个人说话第一个人先说第二个人在说话,第二个人这显示的就是数2了。我想将他们分开,谁和服务器说话只显示他与服务器说话的数目。有谁有想法,最好是有代码,求帮助~!!谢谢了[size=x-small][/size]

  • 写回答

3条回答 默认 最新

  • myali88 2011-08-09 12:12
    关注

    [code="java"]
    /*

    • 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. * */ package org.apache.mina.example.echoserver;

    import java.util.HashMap;
    import java.util.Map;

    import org.apache.mina.core.service.IoHandler;
    import org.apache.mina.core.service.IoHandlerAdapter;
    import org.apache.mina.core.session.IdleStatus;
    import org.apache.mina.core.session.IoSession;
    import org.apache.mina.filter.ssl.SslFilter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    /**

    • {@link IoHandler} implementation for echo server. *
    • @author Apache MINA Project
      */
      public class EchoProtocolHandler extends IoHandlerAdapter {

      private final static Logger LOGGER = LoggerFactory.getLogger(EchoProtocolHandler.class);

      Map lines = new HashMap();

      @Override
      public void sessionCreated(IoSession session) {
      LOGGER.info("CREATED");
      session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);

      // We're going to use SSL negotiation notification.
      session.setAttribute(SslFilter.USE_NOTIFICATION);
      
      lines.put(session, 1);
      

      }

      @Override
      public void sessionClosed(IoSession session) throws Exception {
      LOGGER.info("CLOSED");
      }

      @Override
      public void sessionOpened(IoSession session) throws Exception {
      LOGGER.info("OPENED");
      }

      @Override
      public void sessionIdle(IoSession session, IdleStatus status) {
      LOGGER.info("*** IDLE #" + session.getIdleCount(IdleStatus.BOTH_IDLE) + " ***");
      }

      @Override
      public void exceptionCaught(IoSession session, Throwable cause) {
      session.close(true);
      lines.remove(session);
      }

      @Override
      public void messageReceived(IoSession session, Object message)
      throws Exception {
      LOGGER.info( "Received : " + message );

      Integer last = lines.get(session);
      

      // Reverse reveiced string
      String str = message.toString();
      StringBuilder buf = new StringBuilder(str.length());
      for (int i = str.length() - 1; i >= 0; i--) {
      buf.append(str.charAt(i));
      }

      // and write it back.
      session.write(last + "> : " + buf.toString());
      
      lines.put(session, ++last);
      

      }

    }
    [/code]
    这个是我在minaecho例子上做的一点修改,主要想法就是在Handler中保持不同连接的对话行号,并在每次发送数据后累加。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序