天天看点

XSocket的学习和总结

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实例:

服务端数据处理类:

Java代码  

XSocket的学习和总结
  1. package com.easyway.space.sockets.xsocket;  
  2. import java.io.IOException;  
  3. import java.nio.BufferUnderflowException;  
  4. import java.nio.channels.ClosedChannelException;  
  5. import org.xsocket.MaxReadSizeExceededException;  
  6. import org.xsocket.connection.IConnectHandler;  
  7. import org.xsocket.connection.IConnectionTimeoutHandler;  
  8. import org.xsocket.connection.IDataHandler;  
  9. import org.xsocket.connection.IDisconnectHandler;  
  10. import org.xsocket.connection.IIdleTimeoutHandler;  
  11. import org.xsocket.connection.INonBlockingConnection;  
  12. public class ServerHandler implements IDataHandler ,IConnectHandler ,IIdleTimeoutHandler ,IConnectionTimeoutHandler,IDisconnectHandler {  
  13.     @Override  
  14.     public boolean onConnect(INonBlockingConnection nbc) throws IOException,  
  15.             BufferUnderflowException, MaxReadSizeExceededException {  
  16.         String  remoteName=nbc.getRemoteAddress().getHostName();  
  17.         System.out.println("服务器信息 : 客户端  " + remoteName + " 已经连接...");  
  18.             return true;  
  19.     }  
  20.     @Override  
  21.     public boolean onDisconnect(INonBlockingConnection nbc) throws IOException { 
  22. // String remoteName=nbc.getRemoteAddress().getHostName();

    // System.out.println("服务器信息:客户端 "+remoteName+" 已经断开.");

  23.        return false;  
  24.     }  
  25.     @Override  
  26.     public boolean onData(INonBlockingConnection nbc) throws IOException,  
  27.             BufferUnderflowException, ClosedChannelException,  
  28.             MaxReadSizeExceededException {  
  29.          String data=nbc.readStringByDelimiter("|");  
  30.          nbc.write("--|server:receive data from client sucessful| -----");  
  31.          nbc.flush();  
  32.          System.out.println(data);  
  33.          return true;  
  34.     }  
  35.     @Override  
  36.     public boolean onIdleTimeout(INonBlockingConnection connection) throws IOException {  
  37.         // TODO Auto-generated method stub  
  38.         return false;  
  39.     }  
  40.     @Override  
  41.     public boolean onConnectionTimeout(INonBlockingConnection connection) throws IOException {  
  42.         // TODO Auto-generated method stub  
  43.         return false;  
  44.     }  
  45. }  

服务端类:

Java代码  

XSocket的学习和总结
  1. package com.easyway.space.sockets.xsocket;  
  2. import java.net.InetAddress;  
  3. import java.util.Map;  
  4. import java.util.Map.Entry;  
  5. import org.xsocket.connection.IServer;  
  6. import org.xsocket.connection.Server;  
  7. import org.xsocket.connection.IConnection.FlushMode;  
  8. public class XSocketServer {  
  9.     private static final int PORT = 8014;  
  10.     public static void main(String[] args) throws Exception {  
  11.         //注意其构造方法有多个。一般是使用这种构造方法出来的!  
  12.         //不过要注意一下java.net.InetAddress这个类的使用在初始化的时候需要捕获异常  
  13.         //可能是这个绑定的主机可能不存在之类的异常即UnknowHostNameException  
  14.         InetAddress address=InetAddress.getByName("localhost");  
  15.         //创建一个服务端的对象  
  16.         IServer srv = new Server(address,PORT,new ServerHandler());  
  17.         //设置当前的采用的异步模式  
  18.         srv.setFlushmode(FlushMode.ASYNC);  
  19.        try{  
  20.            // srv.run();   
  21.            // the call will not return  
  22.            // ... or start it by using a dedicated thread  
  23.             srv.start(); // returns after the server has been started  
  24.             System.out.println("服务器" + srv.getLocalAddress() +":"+PORT);   
  25.             Map<String, Class> maps=srv.getOptions();  
  26.             if(maps!=null){  
  27.                 for (Entry<String, Class> entry : maps.entrySet()) {  
  28.                     System.out.println("key= "+entry.getKey()+" value ="+entry.getValue().getName());  
  29.                 }  
  30.             }  
  31.             System.out.println("日志: " + srv.getStartUpLogMessage());  
  32.        }catch(Exception e){  
  33.             System.out.println(e);  
  34.         }  
  35.   }  
  36. }  

客户端数据处理类:

Java代码  

XSocket的学习和总结
  1. package com.easyway.space.sockets.xsocket;  
  2. import java.io.IOException;  
  3. import java.nio.BufferUnderflowException;  
  4. import java.nio.channels.ClosedChannelException;  
  5. import org.xsocket.MaxReadSizeExceededException;  
  6. import org.xsocket.connection.IConnectHandler;  
  7. import org.xsocket.connection.IDataHandler;  
  8. import org.xsocket.connection.IDisconnectHandler;  
  9. import org.xsocket.connection.INonBlockingConnection;  
  10. public class ClientHandler implements IDataHandler ,IConnectHandler ,IDisconnectHandler {  
  11.     @Override  
  12.     public boolean onConnect(INonBlockingConnection nbc) throws IOException,  
  13.             BufferUnderflowException, MaxReadSizeExceededException {  
  14.         String  remoteName=nbc.getRemoteAddress().getHostName();  
  15.         System.out.println("remoteName "+remoteName +" has connected !");  
  16.        return true;  
  17.     }  
  18.     @Override  
  19.     public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {  
  20.         // TODO Auto-generated method stub  
  21.        return false;  
  22.     }  
  23.     @Override  
  24.     public boolean onData(INonBlockingConnection nbc) throws IOException,  
  25.             BufferUnderflowException, ClosedChannelException,  
  26.             MaxReadSizeExceededException {  
  27.          String data=nbc.readStringByDelimiter("|");  
  28.          nbc.write("--|Client:receive data from server sucessful| -----");  
  29.          nbc.flush();  
  30.          System.out.println(data);  
  31.          return true;  
  32.     }  
  33. }  

客户端类:

Java代码  

XSocket的学习和总结
  1. package com.easyway.space.sockets.xsocket;  
  2. import java.io.IOException;  
  3. import org.xsocket.connection.BlockingConnection;  
  4. import org.xsocket.connection.IBlockingConnection;  
  5. import org.xsocket.connection.INonBlockingConnection;  
  6. import org.xsocket.connection.NonBlockingConnection;  
  7. public class XSocketClient {  
  8.     private static final int PORT = 8014;  
  9.     public static void main(String[] args) throws IOException {  
  10.             //采用非阻塞式的连接  
  11.             INonBlockingConnection nbc = new NonBlockingConnection("localhost", PORT, new ClientHandler());  
  12.             //采用阻塞式的连接  
  13.             //IBlockingConnection bc = new BlockingConnection("localhost", PORT);  
  14.              //一个非阻塞的连接是很容易就变成一个阻塞连接  
  15.             IBlockingConnection bc = new BlockingConnection(nbc);  
  16.            //设置编码格式  
  17.             bc.setEncoding("UTF-8");  
  18.             //设置是否自动清空缓存  
  19.             bc.setAutoflush(true);  
  20.             //向服务端写数据信息  
  21.              for (int i = 0; i < 100; i++) {  
  22.                  bc.write(" client | i |love |china !..." +i);  
  23.             }  
  24.              //向客户端读取数据的信息  
  25.            byte[] byteBuffers= bc.readBytesByDelimiter("|", "UTF-8");  
  26.            //打印服务器端信息  
  27.            System.out.println(new String(byteBuffers));  
  28.            //将信息清除缓存,写入服务器端  
  29.            bc.flush();  
  30.            bc.close();  
  31.     }  
  32. }  

继续阅读