minaフレームワーク学習
昨年はMinaフレームでゲートウェイをしたのですが、結局メモを取らず、今年は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.LogLevel;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class MianClient
{
// 192.168.3.102
private static final int PORT = 10010;
public static void main(String[] args) throws IOException
{
IoAcceptor acceptor = new NioSocketAcceptor();
LoggingFilter loggingFilter = new LoggingFilter();
loggingFilter.setSessionClosedLogLevel(LogLevel.NONE);
loggingFilter.setSessionCreatedLogLevel(LogLevel.DEBUG);
loggingFilter.setSessionOpenedLogLevel(LogLevel.INFO);
acceptor.getFilterChain().addLast("logger", loggingFilter);
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
acceptor.setHandler(new ObjectMinaClientHandler());
acceptor.getSessionConfig().setReadBufferSize( 2048 );
acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
acceptor.bind(new InetSocketAddress(PORT));
System.out.println(" , " + PORT);
}
}
package com.roland.controller.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ObjectMinaClientHandler extends IoHandlerAdapter
{
private static Logger logger = LoggerFactory.getLogger(ObjectMinaClientHandler.class);
//
@Override
public void sessionOpened(IoSession session) throws Exception
{
// session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);
System.out.println(" ");
}
@Override
public void messageSent(IoSession session, Object message) throws Exception
{
System.out.println(" ");
}
@Override
public void sessionCreated(IoSession session) throws Exception
{
System.out.println(session.getRemoteAddress().toString());
System.out.println(" ");
}
//
@Override
public void sessionClosed(IoSession session)
{
System.out.println(" ");
}
//
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception
{
System.out.println(" ");
}
public void exceptionCaught(IoSession session, Throwable cause)
{
System.out.println(" ");
System.out.println(cause);
session.close(true);
}
// :
@Override
public void messageReceived(IoSession session, Object message)
{
System.out.println(" " + message.toString());
// session.write(arg0);
}
}
これはサーバー側で、クライアントの今していないで、後で補充します
テストクラス
public class Test
{
public static void main(String[] args)
{
try
{
Socket socket=new Socket("127.0.0.1",10010);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline = "11
";
while(!readline.equals("bye"))
{
os.print(readline);
os.flush();
System.out.println("Client" + readline);
System.out.println("Service" + is.readLine());
readline = sin.readLine();
}
os.close();
is.close();
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}