xSocket是一个易于使用的基于NIO库来构建高性能,可扩展的网络应用。 它支持写入以及服务器端的应用,以直观的方式客户端应用程序。 检测问题,如低水平NIO选择编程,连接池管理,连接超时被封装的xSocket。
我从它的官网上面下载了两个JAR一个是其核心JAR包
xSocket (core)
另外一个JAR包是:xSocket multiplexed
先掌握其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.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;
/**
* 服务端定义数据的处理类
* @author longgangbai
*
*/
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;
}
/**
* 即如果失去连接应当如何处理?
*需要实现 IDisconnectHandler 这个接口
* 连接断开时的操作
*/
@Override
public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {
return false;
}
/**
* 即这个方法不光是说当接收到一个新的网络包的时候会调用而且如果有新的缓存存在的时候也会被调用。而且
*The onData will also be called, if the connection is closed当连接被关闭的时候也会被调用的!
*/
@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 {
// TODO Auto-generated method stub
return false;
}
/**
* 连接超时处理事件
*/
@Override
public boolean onConnectionTimeout(INonBlockingConnection connection) throws IOException {
// TODO Auto-generated method stub
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);
- }
-
- }
-
- }