xSocket是一个易于使用的基于NIO库来构建高性能,可扩展的网络应用。 它支持写入以及服务器端的应用,以直观的方式客户端应用程序。 检测问题,如低水平NIO选择编程,连接池管理,连接超时被封装的xSocket。
我从它的官网上面下载了两个JAR一个是其核心JAR包
xSocket (core)
下载地址是:http://sourceforge.net/projects/xsocket/files/xsocket%202.x/2.7.2/xSocket-2.7.2.jar/download
另外一个JAR包是:xSocket multiplexed
下载地址是http://sourceforge.net/projects/xsocket/files/xsocket%202.x%20-%20multiplexed/2.1.5/xSocket-multiplexed-2.1.5.jar/download (这个JAR包比较小)
先掌握其core部分然后再去学习其扩展部分的功能!
随着xSocket你可以编写高性能,可扩展的客户端和服务器组件的自定义协议如SMTP服务器,代理服务器或客户端和服务器组件是一个基于。
IDataHandler :服务端或者客户端端数据处理类;
IConnectHandler 服务端或者客户端连接成功是处理操作。
IIdleTimeoutHandler 请求处理超时才操作。
IConnectionTimeoutHandler连接超时的操作
IDisconnectHandler 连接断开时的操作
IBlockingConnection 阻塞模式的连接
INonblockingConnection 非阻塞模式的连接
XSocket的ongoing实例:
服务端数据处理类:
- package com.easyway.space.sockets.xsocket;
-
-
- import java.io.IOException;
- import java.nio.BufferUnderflowException;
- import java.nio.channels.ClosedChannelException;
-
- import org.xsocket.MaxReadSizeExceededException;
- import org.xsocket.connection.IConnectHandler;
- import org.xsocket.connection.IConnectionTimeoutHandler;
- import org.xsocket.connection.IDataHandler;
- import org.xsocket.connection.IDisconnectHandler;
- import org.xsocket.connection.IIdleTimeoutHandler;
- import org.xsocket.connection.INonBlockingConnection;
-
-
-
-
-
- public class ServerHandler implements IDataHandler ,IConnectHandler ,IIdleTimeoutHandler ,IConnectionTimeoutHandler,IDisconnectHandler {
-
-
-
-
-
- @Override
- public boolean onConnect(INonBlockingConnection nbc) throws IOException,
- BufferUnderflowException, MaxReadSizeExceededException {
- String remoteName=nbc.getRemoteAddress().getHostName();
- System.out.println("remoteName "+remoteName +" has connected !");
- return true;
- }
-
-
-
-
-
- @Override
- public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {
- return false;
- }
-
-
-
-
- @Override
- public boolean onData(INonBlockingConnection nbc) throws IOException,
- BufferUnderflowException, ClosedChannelException,
- MaxReadSizeExceededException {
- String data=nbc.readStringByDelimiter("|");
- nbc.write("--|server:receive data from client sucessful| -----");
- nbc.flush();
- System.out.println(data);
- return true;
- }
-
-
-
- @Override
- public boolean onIdleTimeout(INonBlockingConnection connection) throws IOException {
-
- return false;
- }
-
-
-
- @Override
- public boolean onConnectionTimeout(INonBlockingConnection connection) throws IOException {
-
- return false;
- }
-
- }
服务端类:
- package com.easyway.space.sockets.xsocket;
-
- import java.net.InetAddress;
- import java.util.Map;
- import java.util.Map.Entry;
-
- import org.xsocket.connection.IServer;
- import org.xsocket.connection.Server;
- import org.xsocket.connection.IConnection.FlushMode;
-
-
-
-
-
-
- public class XSocketServer {
-
-
- private static final int PORT = 8014;
-
- public static void main(String[] args) throws Exception {
-
-
-
- InetAddress address=InetAddress.getByName("localhost");
-
- IServer srv = new Server(address,PORT,new ServerHandler());
-
- srv.setFlushmode(FlushMode.ASYNC);
- try{
-
-
-
- srv.start();
- System.out.println("服务器" + srv.getLocalAddress() +":"+PORT);
- Map<String, Class> maps=srv.getOptions();
- if(maps!=null){
-
- for (Entry<String, Class> entry : maps.entrySet()) {
- System.out.println("key= "+entry.getKey()+" value ="+entry.getValue().getName());
- }
- }
- System.out.println("日志: " + srv.getStartUpLogMessage());
-
- }catch(Exception e){
- System.out.println(e);
- }
-
- }
-
- }
客户端数据处理类:
- package com.easyway.space.sockets.xsocket;
-
-
- import java.io.IOException;
- import java.nio.BufferUnderflowException;
- import java.nio.channels.ClosedChannelException;
-
- import org.xsocket.MaxReadSizeExceededException;
- import org.xsocket.connection.IConnectHandler;
- import org.xsocket.connection.IDataHandler;
- import org.xsocket.connection.IDisconnectHandler;
- import org.xsocket.connection.INonBlockingConnection;
-
-
-
-
-
- public class ClientHandler implements IDataHandler ,IConnectHandler ,IDisconnectHandler {
-
-
-
-
- @Override
- public boolean onConnect(INonBlockingConnection nbc) throws IOException,
- BufferUnderflowException, MaxReadSizeExceededException {
- String remoteName=nbc.getRemoteAddress().getHostName();
- System.out.println("remoteName "+remoteName +" has connected !");
- return true;
- }
-
-
-
- @Override
- public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {
-
- return false;
- }
-
-
-
-
- @Override
- public boolean onData(INonBlockingConnection nbc) throws IOException,
- BufferUnderflowException, ClosedChannelException,
- MaxReadSizeExceededException {
- String data=nbc.readStringByDelimiter("|");
- nbc.write("--|Client:receive data from server sucessful| -----");
- nbc.flush();
- System.out.println(data);
- return true;
- }
-
- }
客户端类:
- package com.easyway.space.sockets.xsocket;
-
- import java.io.IOException;
-
- import org.xsocket.connection.BlockingConnection;
- import org.xsocket.connection.IBlockingConnection;
- import org.xsocket.connection.INonBlockingConnection;
- import org.xsocket.connection.NonBlockingConnection;
-
-
-
-
-
-
-
-
- public class XSocketClient {
- private static final int PORT = 8014;
- public static void main(String[] args) throws IOException {
-
- INonBlockingConnection nbc = new NonBlockingConnection("localhost", PORT, new ClientHandler());
-
-
-
-
- IBlockingConnection bc = new BlockingConnection(nbc);
-
- bc.setEncoding("UTF-8");
-
- bc.setAutoflush(true);
-
- for (int i = 0; i < 100; i++) {
- bc.write(" client | i |love |china !..." +i);
- }
-
- byte[] byteBuffers= bc.readBytesByDelimiter("|", "UTF-8");
-
- System.out.println(new String(byteBuffers));
-
- bc.flush();
- bc.close();
- }
-
- }
===================================================================================================================
XSocket的学习和总结(二)
分类: xsocket
2011-08-10 10:15
951人阅读
收藏
举报
2011年04月08日 星期五 下午 02:49
客户端数据处理类:
- package com.easyway.space.sockets.xsocket;
-
-
- import java.io.IOException;
- import java.nio.BufferUnderflowException;
- import java.nio.channels.ClosedChannelException;
-
- import org.xsocket.MaxReadSizeExceededException;
- import org.xsocket.connection.IConnectHandler;
- import org.xsocket.connection.IDataHandler;
- import org.xsocket.connection.IDisconnectHandler;
- import org.xsocket.connection.INonBlockingConnection;
-
-
-
-
-
- public class ClientHandler implements IDataHandler ,IConnectHandler ,IDisconnectHandler {
-
-
-
-
- @Override
- public boolean onConnect(INonBlockingConnection nbc) throws IOException,
- BufferUnderflowException, MaxReadSizeExceededException {
- String remoteName=nbc.getRemoteAddress().getHostName();
- System.out.println("remoteName "+remoteName +" has connected !");
- return true;
- }
-
-
-
- @Override
- public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {
-
- return false;
- }
-
-
-
-
- @Override
- public boolean onData(INonBlockingConnection nbc) throws IOException,
- BufferUnderflowException, ClosedChannelException,
- MaxReadSizeExceededException {
- String data=nbc.readStringByDelimiter("|");
- nbc.write("--|Client:receive data from server sucessful| -----");
- nbc.flush();
- System.out.println(data);
- return true;
- }
-
- }
- package com.easyway.space.sockets.xsocket;
-
-
- import java.io.IOException;
- import java.nio.BufferUnderflowException;
- import java.nio.channels.ClosedChannelException;
-
- import org.xsocket.MaxReadSizeExceededException;
- import org.xsocket.connection.IConnectHandler;
- import org.xsocket.connection.IDataHandler;
- import org.xsocket.connection.IDisconnectHandler;
- import org.xsocket.connection.INonBlockingConnection;
-
-
-
-
-
- public class ClientHandler implements IDataHandler ,IConnectHandler ,IDisconnectHandler {
-
-
-
-
- @Override
- public boolean onConnect(INonBlockingConnection nbc) throws IOException,
- BufferUnderflowException, MaxReadSizeExceededException {
- String remoteName=nbc.getRemoteAddress().getHostName();
- System.out.println("remoteName "+remoteName +" has connected !");
- return true;
- }
-
-
-
- @Override
- public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {
-
- return false;
- }
-
-
-
-
- @Override
- public boolean onData(INonBlockingConnection nbc) throws IOException,
- BufferUnderflowException, ClosedChannelException,
- MaxReadSizeExceededException {
- String data=nbc.readStringByDelimiter("|");
- nbc.write("--|Client:receive data from server sucessful| -----");
- nbc.flush();
- System.out.println(data);
- return true;
- }
-
- }
客户端类:
- package com.easyway.space.sockets.xsocket;
-
- import java.io.IOException;
-
- import org.xsocket.connection.BlockingConnection;
- import org.xsocket.connection.IBlockingConnection;
- import org.xsocket.connection.INonBlockingConnection;
- import org.xsocket.connection.NonBlockingConnection;
-
-
-
-
-
-
-
-
- public class XSocketClient {
- private static final int PORT = 8014;
- public static void main(String[] args) throws IOException {
-
- INonBlockingConnection nbc = new NonBlockingConnection("localhost", PORT, new ClientHandler());
-
-
-
-
- IBlockingConnection bc = new BlockingConnection(nbc);
-
- bc.setEncoding("UTF-8");
-
- bc.setAutoflush(true);
-
- for (int i = 0; i < 100; i++) {
- bc.write(" client | i |love |china !..." +i);
- }
-
- byte[] byteBuffers= bc.readBytesByDelimiter("|", "UTF-8");
-
- System.out.println(new String(byteBuffers));
-
- bc.flush();
- bc.close();
- }
-
- }
|
代码如上,如有问题,欢迎商讨, 谢谢!