客户端数据处理类:
- 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 {
-
- 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;
- }
-
- }
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;
/**
* 客户端定义数据的处理类
* @author longgangbai
*
*/
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;
}
}
客户端类:
- 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(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();
- }
-
- }