cindy源码阅读(1) hello word
Cindy是一个强壮,可扩展,高效的异步I/O框架。支持TCP,SSL-TCP, UDP和Pipe。和他类似的有mina。
先看一个cindy的hello word,这个是我在官方的例子上做了修改,这个是最简单的了。
可以看到cindy的基本操作就是打开一个session,可以简单理解为就是一个socket,然后向这个 session发送消息。
session.send("hello, world!");,session收到消息之后会执行SessionHandler的objectReceived。objectReceived就可以根据自己的需求自己去写了。
package net.sf.cindy.example.helloworld;import net.sf.cindy.Session;import net.sf.cindy.SessionHandlerAdapter;import net.sf.cindy.decoder.SerialDecoder;import net.sf.cindy.encoder.SerialEncoder;import net.sf.cindy.session.nio.PipeSession;public class HelloWorld { public static void main(String[] args) throws Exception { Session session = new PipeSession(); session.setPacketEncoder(new SerialEncoder()); session.setPacketDecoder(new SerialDecoder()); session.setSessionHandler(new SessionHandlerAdapter() { public void objectReceived(Session session, Object obj)throws Exception { System.out.println("received: " + obj); } }); session.start().complete(); session.send("hello, world!"); }}