在学习mina框架时我重写了public void inputClosed(IoSession is) 方法然后我在关闭客户端连接时出现了这个方法的不断调用 下面是我的代码,请问有人遇到过吗package mina;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
class MyHandler implements IoHandler {
public MyHandler() {
}
@Override
public void sessionCreated(IoSession is) throws Exception {
System.out.println("create");
}
@Override
public void sessionOpened(IoSession is) throws Exception {
System.out.println("opensession");
}
@Override
public void sessionClosed(IoSession is) throws Exception {
System.out.println("session close");
}
@Override
public void sessionIdle(IoSession is, IdleStatus is1) throws Exception {
System.out.println("session idle");
}
@Override
public void exceptionCaught(IoSession is, Throwable thrwbl) throws Exception {
System.out.println("except");
}
@Override
public void messageReceived(IoSession is, Object o) throws Exception {
System.out.println("msg rec"+(String)o);
is.write((String)o);
}
@Override
public void messageSent(IoSession is, Object o) throws Exception {
System.out.println("msg sent");
}
@Override
public void inputClosed(IoSession is) throws Exception {
System.out.println("close");
}
}package mina;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class SocketServer {
public static void main(String[] args) {
try {
NioSocketAcceptor accepter =new NioSocketAcceptor();
accepter.setHandler(new MyHandler());
accepter.getFilterChain().addLast("codec", new ProtocolCodecFilter( new TextLineCodecFactory()));
accepter.bind(new InetSocketAddress(9427));
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}package ps.mc.chartT;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SocketClient {
public static void main(String[] args) {
Socket socket=null;
BufferedReader inputReader=null;
BufferedWriter outputWriter=null;
try {
socket =new Socket("127.0.0.1",9427);
inputReader =new BufferedReader(new InputStreamReader(System.in));
outputWriter =new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String content;
while(!(content=inputReader.readLine()).equals("bye")){
outputWriter.write(content+"\n");
outputWriter.flush();
}
} catch (IOException ex) {
Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
inputReader.close();
outputWriter.close();
socket.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
3 回答
慕UI4579374
TA贡献1条经验 获得超0个赞
@Override
public void inputClosed(IoSession session) throws Exception {
session.closeNow();
}
将session 关闭就可以了。
添加回答
举报
0/150
提交
取消