zoukankan      html  css  js  c++  java
  • XSocket的学习和总结

    xSocket是一个易于使用的基于NIO库来构建高性能,可扩展的网络应用。 它支持写入以及服务器端的应用,以直观的方式客户端应用程序。 检测问题,如低水平NIO选择编程,连接池管理,连接超时被封装的xSocket。  

    我从它的官网上面下载了两个JAR一个是其核心JAR包xSocket (core) 

    另外一个JAR包是:xSocket multiplexed

    先掌握其core部分然后再去学习其扩展部分的功能!

          随着xSocket你可以编写高性能,可扩展的客户端和服务器组件的自定义协议如SMTP服务器,代理服务器或客户端和服务器组件是一个基于。

          IDataHandler :服务端或者客户端端数据处理类;

         IConnectHandler 服务端或者客户端连接成功是处理操作。  

    IIdleTimeoutHandler 请求处理超时才操作。  

    IConnectionTimeoutHandler连接超时的操作

    IDisconnectHandler 连接断开时的操作

    IBlockingConnection 阻塞模式的连接
    INonblockingConnection 非阻塞模式的连接

     

     

    XSocket的ongoing实例:

    服务端数据处理类:

    Java代码  收藏代码
    1. package com.easyway.space.sockets.xsocket;  
    2.   
    3.   
    4. import java.io.IOException;  
    5. import java.nio.BufferUnderflowException;  
    6. import java.nio.channels.ClosedChannelException;  
    7.   
    8. import org.xsocket.MaxReadSizeExceededException;  
    9. import org.xsocket.connection.IConnectHandler;  
    10. import org.xsocket.connection.IConnectionTimeoutHandler;  
    11. import org.xsocket.connection.IDataHandler;  
    12. import org.xsocket.connection.IDisconnectHandler;  
    13. import org.xsocket.connection.IIdleTimeoutHandler;  
    14. import org.xsocket.connection.INonBlockingConnection;  
    15. /** 
    16.  * 服务端定义数据的处理类 
    17.  * @author longgangbai 
    18.  * 
    19.  */  
    20. public class ServerHandler implements IDataHandler ,IConnectHandler ,IIdleTimeoutHandler ,IConnectionTimeoutHandler,IDisconnectHandler {  
    21.   
    22.     /** 
    23.      * 即当建立完连接之后可以进行的一些相关操作处理。包括修改连接属性、准备资源、等! 
    24.      * 连接的成功时的操作 
    25.      */  
    26.     @Override  
    27.     public boolean onConnect(INonBlockingConnection nbc) throws IOException,  
    28.             BufferUnderflowException, MaxReadSizeExceededException {  
    29.         String  remoteName=nbc.getRemoteAddress().getHostName();  
    30.         System.out.println("服务器信息 : 客户端  " + remoteName + " 已经连接...");  
    31.             return true;  
    32.     }  
    33.     /** 
    34.      * 即如果失去连接应当如何处理? 
    35.      *需要实现 IDisconnectHandler  这个接口 
    36.      * 连接断开时的操作 
    37.      */  
    38.     @Override  
    39.     public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {
    40.        

      // String remoteName=nbc.getRemoteAddress().getHostName();

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


    41.        return false;  
    42.     }  
    43.     /** 
    44.      * 即这个方法不光是说当接收到一个新的网络包的时候会调用而且如果有新的缓存存在的时候也会被调用。而且 
    45.      *The onData will also be called, if the connection is closed当连接被关闭的时候也会被调用的! 
    46.      */  
    47.     @Override  
    48.     public boolean onData(INonBlockingConnection nbc) throws IOException,  
    49.             BufferUnderflowException, ClosedChannelException,  
    50.             MaxReadSizeExceededException {  
    51.          String data=nbc.readStringByDelimiter("|");  
    52.          nbc.write("--|server:receive data from client sucessful| -----");  
    53.          nbc.flush();  
    54.          System.out.println(data);  
    55.          return true;  
    56.     }  
    57.     /** 
    58.      * 请求处理超时的处理事件 
    59.      */  
    60.     @Override  
    61.     public boolean onIdleTimeout(INonBlockingConnection connection) throws IOException {  
    62.         // TODO Auto-generated method stub  
    63.         return false;  
    64.     }  
    65.     /** 
    66.      * 连接超时处理事件 
    67.      */  
    68.     @Override  
    69.     public boolean onConnectionTimeout(INonBlockingConnection connection) throws IOException {  
    70.         // TODO Auto-generated method stub  
    71.         return false;  
    72.     }  
    73.   
    74. }  

     

     

    服务端类:

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

     

     

     

    客户端数据处理类:

    Java代码  收藏代码
    1. package com.easyway.space.sockets.xsocket;  
    2.   
    3.   
    4. import java.io.IOException;  
    5. import java.nio.BufferUnderflowException;  
    6. import java.nio.channels.ClosedChannelException;  
    7.   
    8. import org.xsocket.MaxReadSizeExceededException;  
    9. import org.xsocket.connection.IConnectHandler;  
    10. import org.xsocket.connection.IDataHandler;  
    11. import org.xsocket.connection.IDisconnectHandler;  
    12. import org.xsocket.connection.INonBlockingConnection;  
    13. /** 
    14.  * 客户端定义数据的处理类 
    15.  * @author longgangbai 
    16.  * 
    17.  */  
    18. public class ClientHandler implements IDataHandler ,IConnectHandler ,IDisconnectHandler {  
    19.   
    20.     /** 
    21.      * 连接的成功时的操作 
    22.      */  
    23.     @Override  
    24.     public boolean onConnect(INonBlockingConnection nbc) throws IOException,  
    25.             BufferUnderflowException, MaxReadSizeExceededException {  
    26.         String  remoteName=nbc.getRemoteAddress().getHostName();  
    27.         System.out.println("remoteName "+remoteName +" has connected !");  
    28.        return true;  
    29.     }  
    30.     /** 
    31.      * 连接断开时的操作 
    32.      */  
    33.     @Override  
    34.     public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {  
    35.         // TODO Auto-generated method stub  
    36.        return false;  
    37.     }  
    38.     /** 
    39.      *  
    40.      * 接收到数据库时候的处理 
    41.      */  
    42.     @Override  
    43.     public boolean onData(INonBlockingConnection nbc) throws IOException,  
    44.             BufferUnderflowException, ClosedChannelException,  
    45.             MaxReadSizeExceededException {  
    46.          String data=nbc.readStringByDelimiter("|");  
    47.          nbc.write("--|Client:receive data from server sucessful| -----");  
    48.          nbc.flush();  
    49.          System.out.println(data);  
    50.          return true;  
    51.     }  
    52.   
    53. }  

     

     

    客户端类:

    Java代码  收藏代码
    1. package com.easyway.space.sockets.xsocket;  
    2.   
    3. import java.io.IOException;  
    4.   
    5. import org.xsocket.connection.BlockingConnection;  
    6. import org.xsocket.connection.IBlockingConnection;  
    7. import org.xsocket.connection.INonBlockingConnection;  
    8. import org.xsocket.connection.NonBlockingConnection;  
    9. /** 
    10.  * 客户端接收服务端信息 
    11.  * @author longgangbai 
    12.  * IBlockingConnection:这个的话就是不支持事件回调处理机制的! 
    13.  *INonBlockingConnection:这个连接支持回调机制 
    14.  * 
    15.  *非阻塞的客户端是能够支持事件处理的方法的。即如果从网络通道中没有取到想要的数据就会自动退出程序 
    16.  */  
    17. public class XSocketClient {  
    18.     private static final int PORT = 8014;  
    19.     public static void main(String[] args) throws IOException {  
    20.             //采用非阻塞式的连接  
    21.             INonBlockingConnection nbc = new NonBlockingConnection("localhost", PORT, new ClientHandler());  
    22.              
    23.             //采用阻塞式的连接  
    24.             //IBlockingConnection bc = new BlockingConnection("localhost", PORT);  
    25.              //一个非阻塞的连接是很容易就变成一个阻塞连接  
    26.             IBlockingConnection bc = new BlockingConnection(nbc);  
    27.            //设置编码格式  
    28.             bc.setEncoding("UTF-8");  
    29.             //设置是否自动清空缓存  
    30.             bc.setAutoflush(true);  
    31.             //向服务端写数据信息  
    32.              for (int i = 0; i < 100; i++) {  
    33.                  bc.write(" client | i |love |china !..." +i);  
    34.             }  
    35.              //向客户端读取数据的信息  
    36.            byte[] byteBuffers= bc.readBytesByDelimiter("|""UTF-8");  
    37.            //打印服务器端信息  
    38.            System.out.println(new String(byteBuffers));  
    39.            //将信息清除缓存,写入服务器端  
    40.            bc.flush();  
    41.            bc.close();  
    42.     }  
    43.   
    44. }  

     


  • 相关阅读:
    Spring bean的自动装配
    JSP三大指令
    JSP九大内置对象
    Java异常的捕获顺序(多个catch)
    Integer.parseInt(s)和Integer.valueOf(s)之间的区别
    mysql忘记密码(MySQL5.7)
    java的四种内部类
    内存泄露查询
    深度优先和广度优先比较
    循环队列
  • 原文地址:https://www.cnblogs.com/cuker919/p/4878556.html
Copyright © 2011-2022 走看看