My favorites | Sign in
Project Logo
                
Links:
Blogs:
Feeds:
People details
Project owners:
  killme2008

Yet another nio framework for java

Yanf4j is another nio framework for java,it is light weight,simple and have a great performance.It only supports non-blocking mode for tcp/udp server and client,it doesn't supports blocking mode.It needs jdk>=5.0.

Example

A Time Server

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.code.yanf4j.nio.Session;
import com.google.code.yanf4j.nio.impl.HandlerAdapter;

public class TimeHandler extends HandlerAdapter{

	@Override
	public void onSessionStarted(Session session) {
		Date date = new Date();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		session.asyncWrite(dateFormat.format(date));
		session.flush();
		session.close();
		
	}
}
  
import java.io.IOException;
import java.net.InetSocketAddress;

import com.google.code.yanf4j.config.Configuration;
import com.google.code.yanf4j.core.impl.TextLineCodecFactory;
import com.google.code.yanf4j.nio.TCPController;
import com.google.code.yanf4j.nio.impl.SocketChannelController;

public class TimeServer {
	public static void main(String[] args) {
		int port = 8090;
		if (args.length >= 1) {
			port = Integer.parseInt(args[0]);
		}
		SocketChannelController controller = new TCPController(new Configuration(),
				new TimeHandler(), new TextLineCodecFactory());
		try {
			controller.bind(new InetSocketAddress(port));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}








Hosted by Google Code