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. packagecom.easyway.space.sockets.xsocket;
  2. importjava.io.IOException;
  3. importjava.nio.BufferUnderflowException;
  4. importjava.nio.channels.ClosedChannelException;
  5. importorg.xsocket.MaxReadSizeExceededException;
  6. importorg.xsocket.connection.IConnectHandler;
  7. importorg.xsocket.connection.IConnectionTimeoutHandler;
  8. importorg.xsocket.connection.IDataHandler;
  9. importorg.xsocket.connection.IDisconnectHandler;
  10. importorg.xsocket.connection.IIdleTimeoutHandler;
  11. importorg.xsocket.connection.INonBlockingConnection;
  12. /**
  13. *服务端定义数据的处理类
  14. *@authorlonggangbai
  15. *
  16. */
  17. publicclassServerHandlerimplementsIDataHandler,IConnectHandler,IIdleTimeoutHandler,IConnectionTimeoutHandler,IDisconnectHandler{
  18. /**
  19. *即当建立完连接之后可以进行的一些相关操作处理。包括修改连接属性、准备资源、等!
  20. *连接的成功时的操作
  21. */
  22. @Override
  23. publicbooleanonConnect(INonBlockingConnectionnbc)throwsIOException,
  24. BufferUnderflowException,MaxReadSizeExceededException{
  25. StringremoteName=nbc.getRemoteAddress().getHostName();
  26. System.out.println("服务器信息 : 客户端 " + remoteName + " 已经连接...");
  27. returntrue;
  28. }
  29. /**
  30. *即如果失去连接应当如何处理?
  31. *需要实现IDisconnectHandler这个接口
  32. *连接断开时的操作
  33. */
  34. @Override
  35. publicbooleanonDisconnect(INonBlockingConnectionnbc)throwsIOException{
  36. // String remoteName=nbc.getRemoteAddress().getHostName();

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


  37. returnfalse;
  38. }
  39. /**
  40. *即这个方法不光是说当接收到一个新的网络包的时候会调用而且如果有新的缓存存在的时候也会被调用。而且
  41. *TheonDatawillalsobecalled,iftheconnectionisclosed当连接被关闭的时候也会被调用的!
  42. */
  43. @Override
  44. publicbooleanonData(INonBlockingConnectionnbc)throwsIOException,
  45. BufferUnderflowException,ClosedChannelException,
  46. MaxReadSizeExceededException{
  47. Stringdata=nbc.readStringByDelimiter("|");
  48. nbc.write("--|server:receivedatafromclientsucessful|-----");
  49. nbc.flush();
  50. System.out.println(data);
  51. returntrue;
  52. }
  53. /**
  54. *请求处理超时的处理事件
  55. */
  56. @Override
  57. publicbooleanonIdleTimeout(INonBlockingConnectionconnection)throwsIOException{
  58. //TODOAuto-generatedmethodstub
  59. returnfalse;
  60. }
  61. /**
  62. *连接超时处理事件
  63. */
  64. @Override
  65. publicbooleanonConnectionTimeout(INonBlockingConnectionconnection)throwsIOException{
  66. //TODOAuto-generatedmethodstub
  67. returnfalse;
  68. }
  69. }

服务端类:

Java代码 收藏代码
  1. packagecom.easyway.space.sockets.xsocket;
  2. importjava.net.InetAddress;
  3. importjava.util.Map;
  4. importjava.util.Map.Entry;
  5. importorg.xsocket.connection.IServer;
  6. importorg.xsocket.connection.Server;
  7. importorg.xsocket.connection.IConnection.FlushMode;
  8. /**
  9. *采用XSocket通讯的服务端
  10. *@authorlonggangbai
  11. *
  12. */
  13. publicclassXSocketServer{
  14. /**设置当前的端口*/
  15. privatestaticfinalintPORT=8014;
  16. publicstaticvoidmain(String[]args)throwsException{
  17. //注意其构造方法有多个。一般是使用这种构造方法出来的!
  18. //不过要注意一下java.net.InetAddress这个类的使用在初始化的时候需要捕获异常
  19. //可能是这个绑定的主机可能不存在之类的异常即UnknowHostNameException
  20. InetAddressaddress=InetAddress.getByName("localhost");
  21. //创建一个服务端的对象
  22. IServersrv=newServer(address,PORT,newServerHandler());
  23. //设置当前的采用的异步模式
  24. srv.setFlushmode(FlushMode.ASYNC);
  25. try{
  26. //srv.run();
  27. //thecallwillnotreturn
  28. //...orstartitbyusingadedicatedthread
  29. srv.start();//returnsaftertheserverhasbeenstarted
  30. System.out.println("服务器"+srv.getLocalAddress()+":"+PORT);
  31. Map<String,Class>maps=srv.getOptions();
  32. if(maps!=null){
  33. for(Entry<String,Class>entry:maps.entrySet()){
  34. System.out.println("key="+entry.getKey()+"value="+entry.getValue().getName());
  35. }
  36. }
  37. System.out.println("日志:"+srv.getStartUpLogMessage());
  38. }catch(Exceptione){
  39. System.out.println(e);
  40. }
  41. }
  42. }

客户端数据处理类:

Java代码 收藏代码
  1. packagecom.easyway.space.sockets.xsocket;
  2. importjava.io.IOException;
  3. importjava.nio.BufferUnderflowException;
  4. importjava.nio.channels.ClosedChannelException;
  5. importorg.xsocket.MaxReadSizeExceededException;
  6. importorg.xsocket.connection.IConnectHandler;
  7. importorg.xsocket.connection.IDataHandler;
  8. importorg.xsocket.connection.IDisconnectHandler;
  9. importorg.xsocket.connection.INonBlockingConnection;
  10. /**
  11. *客户端定义数据的处理类
  12. *@authorlonggangbai
  13. *
  14. */
  15. publicclassClientHandlerimplementsIDataHandler,IConnectHandler,IDisconnectHandler{
  16. /**
  17. *连接的成功时的操作
  18. */
  19. @Override
  20. publicbooleanonConnect(INonBlockingConnectionnbc)throwsIOException,
  21. BufferUnderflowException,MaxReadSizeExceededException{
  22. StringremoteName=nbc.getRemoteAddress().getHostName();
  23. System.out.println("remoteName"+remoteName+"hasconnected!");
  24. returntrue;
  25. }
  26. /**
  27. *连接断开时的操作
  28. */
  29. @Override
  30. publicbooleanonDisconnect(INonBlockingConnectionnbc)throwsIOException{
  31. //TODOAuto-generatedmethodstub
  32. returnfalse;
  33. }
  34. /**
  35. *
  36. *接收到数据库时候的处理
  37. */
  38. @Override
  39. publicbooleanonData(INonBlockingConnectionnbc)throwsIOException,
  40. BufferUnderflowException,ClosedChannelException,
  41. MaxReadSizeExceededException{
  42. Stringdata=nbc.readStringByDelimiter("|");
  43. nbc.write("--|Client:receivedatafromserversucessful|-----");
  44. nbc.flush();
  45. System.out.println(data);
  46. returntrue;
  47. }
  48. }

客户端类:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值