mina 学习(2)
来源:互联网 发布:优化诊断zhangzifan 编辑:程序博客网 时间:2024/05/21 10:51
上节中通过一个简单的例子,对Mina框架有了大体的了解,在上节的基础上,看看 怎样实现客户端与服务端的通信,
废话不多说了,直接看代码:
- public class Test {
- public static void main(String[] args) throws Exception{
- SocketConnector connector = new NioSocketConnector();
- IoFilter filter = new ProtocolCodecFilter(new TextLineCodecFactory());
- connector.getFilterChain().addLast("vestigge", filter);
- SocketAddress soketAddress = new InetSocketAddress("127.0.0.1", 5469);
- connector.setHandler(new ClientHandler());
- ConnectFuture future= connector.connect(soketAddress);
- future.join();
- if (!future.isConnected()) {
- System.out.println("连接服务器失败");
- return;
- }
- future.getSession().write("hello");
- }
- }
public class Test {public static void main(String[] args) throws Exception{SocketConnector connector = new NioSocketConnector();IoFilter filter = new ProtocolCodecFilter(new TextLineCodecFactory());connector.getFilterChain().addLast("vestigge", filter);SocketAddress soketAddress = new InetSocketAddress("127.0.0.1", 5469);connector.setHandler(new ClientHandler());ConnectFuture future= connector.connect(soketAddress);future.join();if (!future.isConnected()) {System.out.println("连接服务器失败");return;}future.getSession().write("hello");}}
可以看到代码与服务器端的代码很像,也是非常的简单,这就是框架的好处,不用再重复发明轮子,省了不少事,
- public class ClientHandler extends IoHandlerAdapter {
- public void messageReceived(IoSession arg0, Object message) throws Exception {
- System.out.println("收到服务器消息:" + message.toString());
- }
- public void exceptionCaught(IoSession arg0, Throwable arg1)
- throws Exception {
- }
- }
public class ClientHandler extends IoHandlerAdapter {public void messageReceived(IoSession arg0, Object message) throws Exception {System.out.println("收到服务器消息:" + message.toString());}public void exceptionCaught(IoSession arg0, Throwable arg1)throws Exception {}}
效果演示:
0 0
- mina 学习(2)
- apache mina 学习(三)-----Mina架构
- apache mina 学习(三)-----Mina架构
- mina学习2
- mina 学习(1)
- Mina学习(二)
- mina学习(一)
- Mina学习(一)
- mina学习笔记(1)
- MINA框架学习 (一)
- Mina学习(一):mina实现简单服务端与客户端
- Mina学习(二): mina体系结构简要分析
- MINA学习
- MINA学习
- MINA学习
- Mina 学习
- MINA学习
- mina学习
- [LaTex] 上划线输入
- Windows Server 2008企业64位版防火墙添加端口的方法
- Strategy模式
- struts2入门教程一(环境搭建,基本案例实现)
- 《Android游戏开发》11.手把手让你爱上Android sdk自带“9妹”(9patch工具),让Android游戏开发更方便!
- mina 学习(2)
- C#迁移之callXBFLibrary - 2(调用非托管DLL)
- GDAL仿射地理变换参数及设置
- 市场普查项目复核
- 山边小村
- Tomcat启动报错总结
- Node.js 模块 包
- java动态代理学习笔记
- android自动化测试 input