你把那个代码放在哪里了?
我只是像下面那样使用 for 循环,所有会话都已关闭。首先,运行服务器并在 10 秒内启动 3 个客户端。您将看到所有客户端的会话将在 10 秒后关闭。
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class MinaServer {
public static void main(String[] args) throws Exception {
IoAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset
.forName("UTF-8"))));
acceptor.setHandler(new ServerHandler());
acceptor.bind(new InetSocketAddress(1071));
Thread.sleep(10000);
if (acceptor.isActive()) {
for (IoSession ss : acceptor.getManagedSessions().values()) {
ss.close(true);
}
}
}
}