MINA的行号计数

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

3个回答

[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中保持不同连接的对话行号,并在每次发送数据后累加。

可以把计数放在客户端啊!

通过地址和端口区别客户端,然后分别存储就行了吧,每次发言更新下不就可以了吗
使用map存客户端的地址和端口,value存发送次数。lz试试

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问