XSocket的学习和总结(二)

本文介绍了一个使用XSocket的Java客户端示例,展示了如何通过非阻塞连接与服务器交互,并实现数据接收与发送的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  
2011年04月08日 星期五 下午 02:49

客户端数据处理类:

Java代码
  1. package com.easyway.space.sockets.xsocket;   
  2.   
  3.   
  4. import java.io.IOException;   
  5. import java.nio.BufferUnderflowException;   
  6. import java.nio.channels.ClosedChannelException;   
  7.   
  8. import org.xsocket.MaxReadSizeExceededException;   
  9. import org.xsocket.connection.IConnectHandler;   
  10. import org.xsocket.connection.IDataHandler;   
  11. import org.xsocket.connection.IDisconnectHandler;   
  12. import org.xsocket.connection.INonBlockingConnection;   
  13. /**  
  14.  * 客户端定义数据的处理类  
  15.  * @author longgangbai  
  16.  *  
  17.  */  
  18. public class ClientHandler implements IDataHandler ,IConnectHandler ,IDisconnectHandler {   
  19.   
  20.     /**  
  21.      * 连接的成功时的操作  
  22.      */  
  23.     @Override  
  24.     public boolean onConnect(INonBlockingConnection nbc) throws IOException,   
  25.             BufferUnderflowException, MaxReadSizeExceededException {   
  26.         String  remoteName=nbc.getRemoteAddress().getHostName();   
  27.         System.out.println("remoteName "+remoteName +" has connected !");   
  28.        return true;   
  29.     }   
  30.     /**  
  31.      * 连接断开时的操作  
  32.      */  
  33.     @Override  
  34.     public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {   
  35.         // TODO Auto-generated method stub   
  36.        return false;   
  37.     }   
  38.     /**  
  39.      *   
  40.      * 接收到数据库时候的处理  
  41.      */  
  42.     @Override  
  43.     public boolean onData(INonBlockingConnection nbc) throws IOException,   
  44.             BufferUnderflowException, ClosedChannelException,   
  45.             MaxReadSizeExceededException {   
  46.          String data=nbc.readStringByDelimiter("|");   
  47.          nbc.write("--|Client:receive data from server sucessful| -----");   
  48.          nbc.flush();   
  49.          System.out.println(data);   
  50.          return true;   
  51.     }   
  52.   
  53. }  
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;
 }

}

客户端类:

Java代码
  1. package com.easyway.space.sockets.xsocket;   
  2.   
  3. import java.io.IOException;   
  4.   
  5. import org.xsocket.connection.BlockingConnection;   
  6. import org.xsocket.connection.IBlockingConnection;   
  7. import org.xsocket.connection.INonBlockingConnection;   
  8. import org.xsocket.connection.NonBlockingConnection;   
  9. /**  
  10.  * 客户端接收服务端信息  
  11.  * @author longgangbai  
  12.  * IBlockingConnection:这个的话就是不支持事件回调处理机制的!  
  13.  *INonBlockingConnection:这个连接支持回调机制  
  14.  *  
  15.  *非阻塞的客户端是能够支持事件处理的方法的。即如果从网络通道中没有取到想要的数据就会自动退出程序  
  16.  */  
  17. public class XSocketClient {   
  18.     private static final int PORT = 8014;   
  19.     public static void main(String[] args) throws IOException {   
  20.             //采用非阻塞式的连接   
  21.             INonBlockingConnection nbc = new NonBlockingConnection("localhost", PORT, new ClientHandler());   
  22.               
  23.             //采用阻塞式的连接   
  24.             //IBlockingConnection bc = new BlockingConnection("localhost", PORT);   
  25.              //一个非阻塞的连接是很容易就变成一个阻塞连接   
  26.             IBlockingConnection bc = new BlockingConnection(nbc);   
  27.            //设置编码格式   
  28.             bc.setEncoding("UTF-8");   
  29.             //设置是否自动清空缓存   
  30.             bc.setAutoflush(true);   
  31.             //向服务端写数据信息   
  32.              for (int i = 0; i < 100; i++) {   
  33.                  bc.write(" client | i |love |china !..." +i);   
  34.             }   
  35.              //向客户端读取数据的信息   
  36.            byte[] byteBuffers= bc.readBytesByDelimiter("|""UTF-8");   
  37.            //打印服务器端信息   
  38.            System.out.println(new String(byteBuffers));   
  39.            //将信息清除缓存,写入服务器端   
  40.            bc.flush();   
  41.            bc.close();   
  42.     }   
  43.   
  44. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值