1.准备jar包
如果是maven工程:
<!--MINA start -->
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>2.0.2</version>
</dependency>
<!--MINA end -->
如果是普通java工程,jar包如下:
2.开始编程
服务端:
package com.liming.MinaTest;
import java.io.IOException;
import java.net.InetSocketAddress;
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 Main {
public static int port = 9898;
public static void main(String[] args) {
NioSocketAcceptor acceptor = new NioSocketAcceptor();
try {
//设置handler
acceptor.setHandler(new MyHandler());
//设置过滤器
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
//绑定端口号
acceptor.bind(new InetSocketAddress(port));
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.liming.MinaTest;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class MyHandler implements IoHandler {
public void exceptionCaught(IoSession arg0, Throwable arg1)
throws Exception {
System.out.println("exception");
}
public void inputClosed(IoSession arg0) throws Exception {
System.out.println("inputClosed");
}
public void messageReceived(IoSession arg0, Object arg1) throws Exception {
String msg = (String) arg1;
System.out.println("messageReceived server: " + msg);
arg0.write(msg);
}
public void messageSent(IoSession arg0, Object arg1) throws Exception {
System.out.println("messageSent");
}
public void sessionClosed(IoSession arg0) throws Exception {
System.out.println("sessionClosed "+arg0.hashCode());
}
public void sessionCreated(IoSession arg0) throws Exception {
System.out.println("sessionCreated "+arg0.hashCode());
}
public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
System.out.println("sessionIdle "+arg0.hashCode()+" , "+arg1);
}
public void sessionOpened(IoSession arg0) throws Exception {
System.out.println("sessionOpened "+arg0.hashCode());
}
}
客户端:
package com.liming.MinaTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class SocketClient {
public int port = 9898;
public String hostAddress = "127.0.0.1";
public static void main(String[] args) throws IOException {
NioSocketConnector connector = new NioSocketConnector();
connector.setHandler(new MyClientHandler());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1", 9898));
future.awaitUninterruptibly();//等待连接
IoSession session = future.getSession();
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
String inputContent;
while (!(inputContent = inputReader.readLine()).equals("bye")) {
session.write(inputContent);
}
}
}
package com.liming.MinaTest;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class MyClientHandler implements IoHandler {
public void exceptionCaught(IoSession arg0, Throwable arg1)
throws Exception {
System.out.println(arg1.getCause());
}
public void inputClosed(IoSession arg0) throws Exception {
// System.out.println("inputClosed");
}
public void messageReceived(IoSession arg0, Object arg1) throws Exception {
String msg = (String) arg1;
System.out.println("client messageReceived: " + msg);
}
public void messageSent(IoSession arg0, Object arg1) throws Exception {
System.out.println("client messageSent->" + (String)arg1);
}
public void sessionClosed(IoSession arg0) throws Exception {
System.out.println("sessionClosed "+arg0.hashCode());
}
public void sessionCreated(IoSession arg0) throws Exception {
System.out.println("sessionCreated "+arg0.hashCode());
}
public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
System.out.println("sessionIdle "+arg0.hashCode()+" , "+arg1);
}
public void sessionOpened(IoSession arg0) throws Exception {
System.out.println("sessionOpened "+arg0.hashCode());
}
}
3.源码包: