从其官方帮助文档上面介绍:
Data will be read and written by
using a
IBlockingConnection or a
INonblockingConnection object (数据的读写是通过这两个对象实现的!)
DEMO示例
1. a simple tcp/delimiter-based
server example
First, define the handler class which implements the desired handler
interface e.g. IDataHandler, IConnectHandler, IIdleTimeoutHandler or
IConnectionTimeoutHandler. The DataHandler will be called if data has
been received for a connection (which will be passed over).
理解:如果想要开发这种网络通讯程序的话是需要掌握事件处理机制的。即当成功连接成功了怎么处理?当数据获取到了又如何处理?等等所以需要事件处理类的而这些类是必须得实现定义的相关接口如
IDataHandler, IConnectHandler, IIdleTimeoutHandler or
IConnectionTimeoutHandler
EX:
class EchoHandler implements IDataHandler {
//拿到数据之后我们是这样处理这些数据的!
public boolean onData(INonBlockingConnection nbc)
throws IOException,
BufferUnderflowException,
MaxReadSizeExceededException {
String data = nbc.readStringByDelimiter("\r\n");
nbc.write(data + "\r\n");
return true;
}
}
定义了事件处理方法之后就可以写一个服务了。这个就跟Python里面的RPYC是一样的即定义好一个回调事件处理方法
之后就可以进行定义好server对象了!
EX:
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("localhost");
IServer srv = new Server(address,8090,new EchoHandler());
srv.run();
} catch (Exception e) {
System.out.println(e.toString());
}
}
注意其构造方法有多个。一般是使用这种构造方法出来的!
不过要注意一下java.net.InetAddress这个类的使用在初始化的时候需要捕获异常
可能是这个绑定的主机可能不存在之类的异常即UnknowHostNameException
2. Semantic of the DataHandler's
onData method
the onData
method can also be called without receiving new network packets, as
long as xSocket's internal read buffer is not empty.
即这个方法不光是说当接收到一个新的网络包的时候会调用而且如果有新的缓存存在的时候也会被调用。而且
The onData will also be called, if the connection is closed当连接被关闭的时候也会被调用的!
3. Writing a blocking
client
IBlockingConnection bc = new BlockingConnection(host, port);
String req = "Hello server";
bc.write(req + "\r\n");
// read the whole logical part by waiting (blocking) until
// the required data have been received
String res = bc.readStringByDelimiter("\r\n");
assert (req.equals(res));
IBlockingConnection:这个的话就是不支持事件回调处理机制的!
4. Writing a non-blocking
client
By performing the a BlockingConnection's read method, the method call
blocks until data has been received or a time out has been occurred
(刚才我试过了 把服务器输出 的数据格式与客房端上传的数据格式不一样导致了 客户端一直在阻塞状态了直到设置的超时值才停止。这样感觉好像不太好。要一直等)
非阻塞的客户端是能够支持事件处理的方法的。即如果从网络通道中没有取到想要的数据就会自动退出程序
public class ClientMain {
public static void main(String[] args) {
try {
IDataHandler clientHandler = new IDataHandler() { public boolean onData(INonBlockingConnection nbc) throws IOException,BufferUnderflowException,MaxReadSizeExceededException { String res = nbc.readStringByDelimiter("\r\n"); System.out.println(res); return true; } }; //定义一个事件处理类。原来JAVA中也可以这样来定义的跟JS是一样的哦! INonBlockingConnection nbc = new NonBlockingConnection("localhost",8090,
clientHandler);
nbc.write("hello" + "\r\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
一个非阻塞的连接是很容易就变成一个阻塞连接的看下面的代码:
INonBlockingConnection nbc = ... //构造出来一个非阻塞对象
// wrapping an existing non-blocking connection by a BlockingConnection
IBlockingConnection bc = new BlockingConnection(nbc);//得到一个阻塞对象即实现一个非阻塞对象转换成一个阻塞对象!
bc.readInt();//又会进入一个新的阻塞期!
以上都是讲了一些在客户端的阻塞编程与非阻塞编程技术。下面讲一下在服务端的阻塞与非阻塞编程技术!
6. Wrap a non-blocking connection
on the server-side
Typically the onConnect()
method will be used to create a BlockingConnection on the server-side.
class ConnectHandler implements IConnectHandler {
public boolean onConnect(INonBlockingConnection nbc) throws IOException {
IBlockingConnection bc = new BlockingConnection(nbc);
//
return true;
}
7. Handling
connect
如果要对连接进行一些处理。可以实现这个接口
IConnectHandler
public boolean onConnect(INonBlockingConnection nbc) throws IOException {
//... e.g. open resources
return true;
}
通常是能够在这个方法里面去做 checks based on the new connection, to modifiy
connection properties, to prepare resources, to set up backend
connections or to write greeting messages
即当建立完连接之后可以进行的一些相关操作处理。包括修改连接属性、准备资源、等!
8. Handling
disconnect
即如果失去连接应当如何处理?
需要实现
IDisconnectHandler 这个接口
A disconnect occurs in three ways:
- the client-side initiates the disconnect by active closing the
connection. In this case the onDisconnect method will be called
immediately
- the connection breaks or the peer disconnects improperly and the
Java VM detects the broken connection. In this case the
onDisconnect method will be called, when the broken connection is
detected
- the connection breaks or the peer disconnects improperly and the
JavaVM doesn't detect the broken connection. In this case
the onDisconnect method will not be called. To handle this
scenario the handler should implement the IIdleTimeoutHandler
and/or IConnectionTimeoutHandler interface.(如果有一些异常没有办法探测到的话就要考虑走超时机制了!)
什么时候会触发这个事件呢?
9. Asynchronous Connect