xsocket基础学习一
- 博客分类:
- java
1、这个开源的框架只需要有一个J2SE5.0就行了不需要其他的JAR支持。
我从它的官网上面下载了两个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部分然后再去学习其扩展部分的功能!
从其官方帮助文档上面介绍:
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
http://xsocket.sourceforge.net/
我从它的官网上面下载了两个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部分然后再去学习其扩展部分的功能!
从其官方帮助文档上面介绍:
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
http://xsocket.sourceforge.net/
发表评论
-
MyBatis的动态SQL详解
2012-08-19 20:44 712MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我 ... -
spring-动态装配bean
2012-03-19 20:04 915http://uu011.iteye.com/admin/bl ... -
XSocket的学习和总结(一)
2012-03-14 14:20 701http://blog.csdn.net/lizhi20040 ... -
io与nio的区别
2012-03-14 09:52 808io:提供读写操作 nio提供读写操作,基本单位为byteB ... -
详解FreeMarker生成复杂Word文档
2012-03-03 11:57 915http://www.java.sh/web/freemark ... -
java生成word,html文件并将内容保存至数据库
2012-03-03 11:32 945在最近的一个项目中需要将一段字符类型的文本存为word,htm ... -
Jacob解决Word文档的读写问题
2012-03-03 11:19 1078转于:北极之光的博客 标签:jacob word com 表格 ... -
Jacob解析word
2012-03-03 11:17 799由于项目需要,取得word的内容,因研究了下Jacob,尽管P ... -
Java服务器端List对象转换为JSON对象并返回客户端实例
2011-04-26 01:53 814http://apps.hi.baidu.com/share/ ... -
如何将tomcat启动注册为系统服务
2011-04-06 04:02 895曾经做过J2EE开发的朋友,一定对Tomcat这个东西最熟 ... -
添加tomcat 服务删除
2011-04-06 03:15 1308tomcat服务删除 关键字: tomcat服务删除 命令行 ... -
如何用Java操作Word, Excel, PDF文档
2011-04-05 03:27 726http://java.ccidnet.com/art/353 ... -
javascript 关闭
2011-03-31 10:32 0http://www.abab123.com/bbs/down ... -
定数显示通知
2011-03-30 11:56 0window.setInterval('shows()',10 ... -
两个LIST比对筛选返回LIST比对结果(转)
2011-03-28 11:09 1268两个LIST比对筛选返回LIST比对结果 不知道大家有没有碰到 ... -
map 转换Set遍历
2011-03-28 10:25 931Map map=new HashMap(); map.put( ... -
java实现mysql 数据库备份
2011-03-21 16:55 1110//db数据库 //ip 备份数据库地址 //user用户名 ...
相关推荐
XSocket的学习和总结 应用服务器网络应用网络协议.net编程 xSocket是一个易于使用的基于NIO库来构建高性能,可扩展的网络应用。 它支持写入以及服务器端的应用,以直观的方式客户端应用程序。 检测问题,如低水平...
常见NIO开源框架(MINA、xSocket)学习 基于io包的阻塞式socket通信代码简单,在连接数很少的情况下是一个不错的选择。不过实际应用中一个socket服务器采用传统的阻塞式socket方式通信可能会是一场灾难,一路...
xSocket api 2.6.6version
NIO网络框架 xSocket
xsocket NIO框架示例 resources 中有相关的 资料。telnet服务测试教程。和相关jar
xSocket-2.5.4-sources.jar , 2.5.4版的源代码jar包,引入项目即可查看
socket通讯框架xsocket所需的jar包
xSocket是一个轻量级的基于nio的服务器框架用于开发高性能、可扩展、多线程的服务器。该框架封装了线程处理、异步读/写等方面。
轻量级JAVA scoket 服务器XSOCKET
xsocket使用指南, xSocket是一个轻量级的基于nio的服务器框架用于开发高性能、可扩展、多线程的服务器。该框架封装了线程处理、异步读写等方面。
下载即用,springboot集成tcp的一个示例,采用了xsocket完美展示
java源码:NIO网络框架 xSocket.rar
socket,xscoket技术详解,包括两个实例,服务端客户端相互收发信息,建立监听,建立连接等
XSocket vs2017编译测试通过 实现的__功能__有:TCPIP 协议实现 Client 与Server端源码,包含Multicast。
websocket的后台通讯实现,在后台实现server和client通过websocket进行通讯,包括开源包:xsocket、xlightweb两种不同的实现
Xsocket_V2_8_15.rar 一个开源的基于TCP的Socket通信框架,基于java.nio开发的框架。 最好学的一个框架了。 内含开发包、源码和javadoc,javadoc用htmlParser...
了基本的注释,详细的没有,纯手工!
xSocket-multiplexed-2.1.5-sources.jarxSocket-multiplexed-2.1.5-sources.jarxSocket-multiplexed-2.1.5-sources.jarxSocket-multiplexed-2.1.5-sources.jarxSocket-multiplexed-2.1.5-sources.jarxSocket-...
xSocket-2.1.2-sources.jar xSocket-2.7.2.jar xSocket-multiplexed-2.1.5.jar
基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络...