mina下载路径
http://mina.apache.org/downloads.html
apache-mina-2.0.7-bin
传送字符串的简单c/s
所需jar包
mina-core-2.0.7.jar
slf4j-api-1.5.8.jar
slf4j-nop-1.5.8.jar
服务器端
示例代码如下
/**
* mina服务器端
*
* 启动服务器的主类
*
* @time 下午3:17:08
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MinaServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// 创建一个非阻塞的sserver端socket
SocketAcceptor acceptor = new NioSocketAcceptor();
// 创建接收数据的过滤器
DefaultIoFilterChainBuilder chainBuilder =
acceptor.getFilterChain();
// 过滤器读取数据
chainBuilder.addLast("myChain", new ProtocolCodecFilter
(new TextLineCodecFactory()));
// 设定服务器端的消息处理器
acceptor.setHandler(new SampleMinaServerHandler());
int bindPort = 9988;
// 服务器端邦定的端口
acceptor.bind(new InetSocketAddress(bindPort));
System.out.println("minaServer is starting!" + bindPort);
}
}
/**
* 消息处理器
*
* @time 下午3:18:57
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SampleMinaServerHandler extends IoHandlerAdapter {
private int count = 0;
/**
* 客户端连接成功
*/
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("incomming client:" +
session.getRemoteAddress());
}
/**
* 客户端关闭联接
*/
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("one client disconnect!");
}
/**
* 接收到客户端发送的消息
*/
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = (String) message;
System.out.println("接收到客户端消息:" + msg);
// 回送消息给客户端
session.write(msg + count);
count++;
}
}
测试服务器端程序
命令行模式:
telnet localhost 9988
helloworld
服务器端信息
minaServer is starting!9988
incomming client:/127.0.0.1:31185
接收到客户端消息:hello world
代码实现客户端程序
/**
* mina客户端
*
* @time 下午4:04:27
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MinaClient {
/**
* @param args
*/
public static void main(String[] args) {
//
NioSocketConnector connector = new NioSocketConnector();
// 创建过滤器
DefaultIoFilterChainBuilder chain =
connector.getFilterChain();
// 设置过滤器读取数据方式
chain.addLast("myChain", new ProtocolCodecFilter(new
TextLineCodecFactory()));
// 设置客户端的消息处理器
connector.setHandler(new SampleMinaServerHandler());
// 设置超时时间
connector.setConnectTimeout(3000);
// 连接到服务器
ConnectFuture future = connector.connect(new
InetSocketAddress("localhost", 9988));
future.awaitUninterruptibly();
future.getSession().getCloseFuture().awaitUninterruptibly
();
connector.dispose();
}
}
/**
* 客户端消息处理器
*
* @time 下午3:18:57
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SampleMinaServerHandler extends IoHandlerAdapter {
private int count = 0;
/**
* 客户端连接成功
*/
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("incomming client:" +
session.getRemoteAddress());
session.write("client ====come on !");
}
/**
* 客户端关闭联接
*/
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("client ====one client disconnect!");
}
/**
* 接收到客户端发送的消息
*/
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = (String) message;
System.out.println("接收到服务器端消息:" + msg);
// 回送消息给客户端
session.write(msg + count);
count++;
}
}
http://mina.apache.org/downloads.html
apache-mina-2.0.7-bin
传送字符串的简单c/s
所需jar包
mina-core-2.0.7.jar
slf4j-api-1.5.8.jar
slf4j-nop-1.5.8.jar
服务器端
示例代码如下
/**
* mina服务器端
*
* 启动服务器的主类
*
* @time 下午3:17:08
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MinaServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// 创建一个非阻塞的sserver端socket
SocketAcceptor acceptor = new NioSocketAcceptor();
// 创建接收数据的过滤器
DefaultIoFilterChainBuilder chainBuilder =
acceptor.getFilterChain();
// 过滤器读取数据
chainBuilder.addLast("myChain", new ProtocolCodecFilter
(new TextLineCodecFactory()));
// 设定服务器端的消息处理器
acceptor.setHandler(new SampleMinaServerHandler());
int bindPort = 9988;
// 服务器端邦定的端口
acceptor.bind(new InetSocketAddress(bindPort));
System.out.println("minaServer is starting!" + bindPort);
}
}
/**
* 消息处理器
*
* @time 下午3:18:57
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SampleMinaServerHandler extends IoHandlerAdapter {
private int count = 0;
/**
* 客户端连接成功
*/
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("incomming client:" +
session.getRemoteAddress());
}
/**
* 客户端关闭联接
*/
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("one client disconnect!");
}
/**
* 接收到客户端发送的消息
*/
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = (String) message;
System.out.println("接收到客户端消息:" + msg);
// 回送消息给客户端
session.write(msg + count);
count++;
}
}
测试服务器端程序
命令行模式:
telnet localhost 9988
helloworld
服务器端信息
minaServer is starting!9988
incomming client:/127.0.0.1:31185
接收到客户端消息:hello world
代码实现客户端程序
/**
* mina客户端
*
* @time 下午4:04:27
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MinaClient {
/**
* @param args
*/
public static void main(String[] args) {
//
NioSocketConnector connector = new NioSocketConnector();
// 创建过滤器
DefaultIoFilterChainBuilder chain =
connector.getFilterChain();
// 设置过滤器读取数据方式
chain.addLast("myChain", new ProtocolCodecFilter(new
TextLineCodecFactory()));
// 设置客户端的消息处理器
connector.setHandler(new SampleMinaServerHandler());
// 设置超时时间
connector.setConnectTimeout(3000);
// 连接到服务器
ConnectFuture future = connector.connect(new
InetSocketAddress("localhost", 9988));
future.awaitUninterruptibly();
future.getSession().getCloseFuture().awaitUninterruptibly
();
connector.dispose();
}
}
/**
* 客户端消息处理器
*
* @time 下午3:18:57
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SampleMinaServerHandler extends IoHandlerAdapter {
private int count = 0;
/**
* 客户端连接成功
*/
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("incomming client:" +
session.getRemoteAddress());
session.write("client ====come on !");
}
/**
* 客户端关闭联接
*/
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("client ====one client disconnect!");
}
/**
* 接收到客户端发送的消息
*/
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = (String) message;
System.out.println("接收到服务器端消息:" + msg);
// 回送消息给客户端
session.write(msg + count);
count++;
}
}