xSocket是一個易于使用的基于NIO庫來建構高性能,可擴充的網絡應用。 它支援寫入以及伺服器端的應用,以直覺的方式用戶端應用程式。 檢測問題,如低水準NIO選擇程式設計,連接配接池管理,連接配接逾時被封裝的xSocket。
我從它的官網上面下載下傳了兩個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部分然後再去學習其擴充部分的功能!
随着xSocket你可以編寫高性能,可擴充的用戶端和伺服器元件的自定義協定如SMTP伺服器,代理伺服器或用戶端和伺服器元件是一個基于。
IDataHandler :服務端或者用戶端端資料處理類;
IConnectHandler 服務端或者用戶端連接配接成功是處理操作。
IIdleTimeoutHandler 請求處理逾時才操作。
IConnectionTimeoutHandler連接配接逾時的操作
IDisconnectHandler 連接配接斷開時的操作
IBlockingConnection 阻塞模式的連接配接
INonblockingConnection 非阻塞模式的連接配接
XSocket的ongoing執行個體:
服務端資料處理類:
Java代碼

- 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 + " 已經連接配接...");
- return true;
- }
- @Override
- public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {
-
// String remoteName=nbc.getRemoteAddress().getHostName();
// System.out.println("伺服器資訊:用戶端 "+remoteName+" 已經斷開.");
- 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 {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean onConnectionTimeout(INonBlockingConnection connection) throws IOException {
- // TODO Auto-generated method stub
- return false;
- }
- }
服務端類:
Java代碼

- 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 {
- //注意其構造方法有多個。一般是使用這種構造方法出來的!
- //不過要注意一下java.net.InetAddress這個類的使用在初始化的時候需要捕獲異常
- //可能是這個綁定的主機可能不存在之類的異常即UnknowHostNameException
- InetAddress address=InetAddress.getByName("localhost");
- //建立一個服務端的對象
- IServer srv = new Server(address,PORT,new ServerHandler());
- //設定目前的采用的異步模式
- srv.setFlushmode(FlushMode.ASYNC);
- try{
- // srv.run();
- // the call will not return
- // ... or start it by using a dedicated thread
- srv.start(); // returns after the server has been started
- 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);
- }
- }
- }
用戶端資料處理類:
Java代碼

- 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.IDataHandler;
- import org.xsocket.connection.IDisconnectHandler;
- import org.xsocket.connection.INonBlockingConnection;
- public class ClientHandler implements IDataHandler ,IConnectHandler ,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 {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean onData(INonBlockingConnection nbc) throws IOException,
- BufferUnderflowException, ClosedChannelException,
- MaxReadSizeExceededException {
- String data=nbc.readStringByDelimiter("|");
- nbc.write("--|Client:receive data from server sucessful| -----");
- nbc.flush();
- System.out.println(data);
- return true;
- }
- }
用戶端類:
Java代碼

- package com.easyway.space.sockets.xsocket;
- import java.io.IOException;
- import org.xsocket.connection.BlockingConnection;
- import org.xsocket.connection.IBlockingConnection;
- import org.xsocket.connection.INonBlockingConnection;
- import org.xsocket.connection.NonBlockingConnection;
- public class XSocketClient {
- private static final int PORT = 8014;
- public static void main(String[] args) throws IOException {
- //采用非阻塞式的連接配接
- INonBlockingConnection nbc = new NonBlockingConnection("localhost", PORT, new ClientHandler());
- //采用阻塞式的連接配接
- //IBlockingConnection bc = new BlockingConnection("localhost", PORT);
- //一個非阻塞的連接配接是很容易就變成一個阻塞連接配接
- IBlockingConnection bc = new BlockingConnection(nbc);
- //設定編碼格式
- bc.setEncoding("UTF-8");
- //設定是否自動清空緩存
- bc.setAutoflush(true);
- //向服務端寫資料資訊
- for (int i = 0; i < 100; i++) {
- bc.write(" client | i |love |china !..." +i);
- }
- //向用戶端讀取資料的資訊
- byte[] byteBuffers= bc.readBytesByDelimiter("|", "UTF-8");
- //列印伺服器端資訊
- System.out.println(new String(byteBuffers));
- //将資訊清除緩存,寫入伺服器端
- bc.flush();
- bc.close();
- }
- }