package mina;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
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 MinaTimeServer {
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
//This filter will log all information such as newly created
//sessions, messages received, messages sent, session closed
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
//This filter will translate binary or protocol specific data into
//message object and vice versa. We use an existing TextLine
//factory because it will handle text base message for you (
//you don't have to write the codec part)
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(
new TextLineCodecFactory(Charset.forName("UTF-8"))));
acceptor.setHandler(new TimeServerHandler());
acceptor.getSessionConfig().setReadBufferSize(BUF_SIZE);
//the first parameter defines what actions to check for when
//determining if a session is idle, the second parameter defines
//the length of time in seconds that must occur before a session
//is deemed to be idle.
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
acceptor.bind(new InetSocketAddress(PORT));
}
private static final int PORT = 8181,BUF_SIZE = 2048;
}
package mina;
import java.util.Date;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class TimeServerHandler extends IoHandlerAdapter {
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
cause.printStackTrace();
}
@SuppressWarnings("deprecation")
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
System.out.println("receve");
String str = message.toString();
System.out.println("receve:"+str);
if(str.trim().equalsIgnoreCase("quit")){
session.close();
return;
}
Date date = new Date();
session.write(date.toString());
System.out.println("receive and send.");
}
@Override
public void messageSent(IoSession session, Object message) throws Exception
{
super.messageSent(session, message);
}
@Override
public void sessionClosed(IoSession session) throws Exception {
super.sessionClosed(session);
System.out.println("新客户端断开");
}
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println("新客户端连接");
super.sessionCreated(session);
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println("连接空闲");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
// TODO Auto-generated method stub
System.out.println("当连接后打开时触发此方法");
super.sessionOpened(session);
}
}
--
View this message in context: http://apache-mina.10907.n7.nabble.com/mina-receive-tp38218p38233.html
Sent from the Apache MINA Developer Forum mailing list archive at Nabble.com.
|