サーバーを作る
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 {```
// port -》》
``` private static final int PORT = 6488;```
public static void main(String[] args) throws IOException {
// acceptor
IoAcceptor acceptor = new NioSocketAcceptor();
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
// logger
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
// Code
acceptor.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// server
acceptor.setHandler(new TimeServerHandler());
// port
acceptor.bind(new InetSocketAddress(PORT));
// run
acceptor.bind();
}
```}```
---------------------------------------------------------------- :kissing:
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
/**
* サーバー server
*/
public class TimeServerHandler extends IoHandlerAdapter {
/**
* connection
*/
@Override
public void sessionCreated(IoSession session){
// enable ip address and port
System.out.println(session.getRemoteAddress().toString());
}
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
cause.printStackTrace();
}
/**
* message
*/
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
String strMsg = message.toString();
if (strMsg.trim().equalsIgnoreCase("quit")) {
session.close(true);
return;
}
// return message by string
session.write("Hi Client!");
// print message
System.out.println("Message written : " + strMsg);
}
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
System.out.println("IDLE" + session.getIdleCount(status));
}
}
-------------------------------------------------- :sleeping:
Client
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.future.ConnectFuture;
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.NioSocketConnector;
public class MinaTimeClient {
public static void main(String[] args){
// create user connection contianer
NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// set outTime
connector.setConnectTimeoutCheckInterval(30);
connector.setHandler(new TimeClientHandler());
// create link
ConnectFuture cf = connector.connect(new InetSocketAddress("192.168.2.109", 6488));
// wait for link will be connection
cf.awaitUninterruptibly();
cf.getSession().write("Hi Server!");
cf.getSession().write("quit");
// wait link offline
cf.getSession().getCloseFuture().awaitUninterruptibly();
// discharge
connector.dispose();
}
}
----------------------------------------------- :unamused:
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
public class TimeClientHandler extends IoHandlerAdapter {
public void messageReceived(IoSession session, Object message) throws Exception {
String content = message.toString();
System.out.println("client receive a message is : " + content);
}
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println("messageSent -> :" + message);
}
}
これは 簡単なDEMOでした〜
![pixelavatar48.png](https://qiita-image-store.s3.amazonaws.com/0/85191/ec3db6eb-53f1-599b-19ac-25cfc7f15b73.png)