minaのいくつかの簡単な例
10460 ワード
minaがサーバに文字列を送信し、サーバが読み出す例、2つのファイル
package test2;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class MainServer {
public static void main(String args[]) throws IOException{
// Server socket NIO
SocketAcceptor acceptor = new NioSocketAcceptor();
//
DefaultIoFilterChainBuilder chain= acceptor.getFilterChain();
// (/r/n)
chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory()));
// new
acceptor.setHandler(new SamplMinaServerHandler());
//
int bindport=9988;
// ,
acceptor.bind(new InetSocketAddress(bindport));
System.out.println("start ok,listen on:="+bindport);
}
}
package test2;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class SamplMinaServerHandler extends IoHandlerAdapter {
public SamplMinaServerHandler() {
// TODO Auto-generated constructor stub
}
@Override
public void sessionOpened(IoSession session )throws Exception{
System.out.println("sessionOpened incomming client :"+session.getRemoteAddress());
}
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
System.out.println("exceptionCaught");
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
// , string
String str=(String)message;
System.out.println(" :"+str);
//
session.write(str);
System.out.println("messageReceived");
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
// TODO Auto-generated method stub
System.out.println("messageSent");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("sessionClose");
}
@Override
public void sessionCreated(IoSession session) throws Exception {
// TODO Auto-generated method stub
System.out.println("sessionCreate");
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
// TODO Auto-generated method stub
System.out.println("sessionIdle");
}
}
この2つはクライアントであり、サーバに接続するために使用されます.
package client;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class MyClient {
public static void main(String args[]){
//create tcp/ip connector
IoConnector connector = new NioSocketConnector();
//
DefaultIoFilterChainBuilder chain=connector.getFilterChain();
// (/r/n)
chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory()));
//
//connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
// :new clinethandler
connector.setHandler(new SamplMinaClientHandler());
//set connect timeout
connector.setConnectTimeoutMillis(30000);
// connector.setConnectTimeout(30);
//
ConnectFuture cf= connector.connect(new InetSocketAddress("localhost",9988));
//wait for the connection attem to be finished
cf.awaitUninterruptibly();
cf.getSession().getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
}
package client;
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;
public class SamplMinaClientHandler extends IoHandlerAdapter implements
IoHandler {
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
// TODO Auto-generated method stub
super.exceptionCaught(session, cause);
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
// ,( 。 String)
String s=(String)message;
// write the receive date back to remote peer
System.out.println(" "+s);
session.write(s);
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
// TODO Auto-generated method stub
super.messageSent(session, message);
}
@Override
public void sessionClosed(IoSession session) throws Exception {
// TODO Auto-generated method stub
System.out.println("sessionClosed");
}
@Override
public void sessionCreated(IoSession session) throws Exception {
// TODO Auto-generated method stub
super.sessionCreated(session);
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
// TODO Auto-generated method stub
super.sessionIdle(session, status);
}
@Override
public void sessionOpened(IoSession session) throws Exception {
// TODO Auto-generated method stub
System.out.println("incomming client"+session.getRemoteAddress());
session.write(" ...");
}
}
minaの簡単な例タイムサーバは、最も基本的な機能を実現します.
package server;
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.example.gettingstarted.timeserver.TimeServerHandler;
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 {
private static final int PORT=6489;
public static void main(String args[]) throws IOException{
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 TimeServerHandler());
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
acceptor.bind(new InetSocketAddress(PORT));
System.out.println("service start");
}
}
package server;
import java.sql.Date;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class TimeServerHandler implements IoHandler {
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
// TODO Auto-generated method stub
cause.printStackTrace();
}
public void messageReceived(IoSession session, Object message) throws Exception {
// TODO Auto-generated method stub
String strMsg= message.toString();
if(strMsg.trim().equalsIgnoreCase("quit")){
session.close();
return;
}
Date date = new Date(0);
session.write(date.toString());
System.out.println("message writen...");
}
public void messageSent(IoSession arg0, Object arg1) throws Exception {
// TODO Auto-generated method stub
}
public void sessionClosed(IoSession arg0) throws Exception {
// TODO Auto-generated method stub
}
public void sessionCreated(IoSession arg0) throws Exception {
// TODO Auto-generated method stub
}
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
System.out.println("IDLE"+session.getIdleCount(status));
}
public void sessionOpened(IoSession arg0) throws Exception {
// TODO Auto-generated method stub
}
}