首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

基于XSocket框架的socket编程技艺(同步通信)

2012-11-03 
基于XSocket框架的socket编程技巧(同步通信)bc.write(msg) //发送信息//接收ByteBuffer byteBuffer Byt

基于XSocket框架的socket编程技巧(同步通信)

  bc.write(msg); //发送信息
   
  //接收
  ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
  int length = bc.read(byteBuffer);
  logger.info("length: " + length);
  byteBuffer.flip();
  byte[] content = new byte[byteBuffer.limit()];
  byteBuffer.get(content); // 从ByteBuffer中读取数据到byte数组中
  logger.info(new String(content));
  byteBuffer.clear();
  bc.flush();
  // always close the connection! (the connection will be returned into the connection pool)
  bc.close(); 
  } catch (IOException ioe) {
  logger.info(ioe.toString());
  if (bc != null) {
  try {
  // if the connection is invalid -> destroy it , it will not return to the pool)
  pool.destroy(bc);
  } catch (Exception ignore) {
  }
  }
  }
}
 
private static void displayPoolInfo(){
  logger.info("--------------------");
  logger.info("getMaxActive:" + pool.getMaxActive());
  logger.info("getMaxActivePerServer:" + pool.getMaxActivePerServer());
  logger.info("getMaxIdle:" + pool.getMaxIdle());
  logger.info("getNumIdle:" + pool.getNumIdle());
  logger.info("getNumActive:" + pool.getNumActive());
  logger.info("getNumCreated:" + pool.getNumCreated());
  logger.info("getNumDestroyed:" + pool.getNumDestroyed());
  logger.info("getNumPendingGet:" + pool.getNumPendingGet());
  logger.info("getNumTimeoutPooledMaxIdleTime:" + pool.getNumTimeoutPooledMaxIdleTime());
  logger.info("getNumTimeoutPooledMaxLifeTime:" + pool.getNumTimeoutPooledMaxLifeTime());
  logger.info("getPooledMaxIdleTimeMillis:" + pool.getPooledMaxIdleTimeMillis());
  logger.info("getPooledMaxLifeTimeMillis:" + pool.getPooledMaxLifeTimeMillis());
  logger.info("isOpen:" + pool.isOpen());
  for (String str: pool.getActiveConnectionInfos()){
  logger.info(str);
  
  }
  logger.info("--------------------");
  
}
 
public static void main(String[] args) throws Exception {
  for (int i = 0; i < 5; i++){
  sendMessage("127.0.0.1",8889,"Send messsage to server.");
  Thread.sleep(1000);
  }
  displayPoolInfo();
  
}
}

And the log is :

INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C1I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C2I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C3I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C4I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C5I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:63) - --------------------
INFO (ConnectionPool.java:64) - getMaxActive:10000
INFO (ConnectionPool.java:65) - getMaxActivePerServer:2147483647
INFO (ConnectionPool.java:66) - getMaxIdle:5000
INFO (ConnectionPool.java:67) - getNumIdle:0
INFO (ConnectionPool.java:68) - getNumActive:5
INFO (ConnectionPool.java:69) - getNumCreated:5
INFO (ConnectionPool.java:70) - getNumDestroyed:0
INFO (ConnectionPool.java:71) - getNumPendingGet:0
INFO (ConnectionPool.java:72) - getNumTimeoutPooledMaxIdleTime:0
INFO (ConnectionPool.java:73) - getNumTimeoutPooledMaxLifeTime:0
INFO (ConnectionPool.java:74) - getPooledMaxIdleTimeMillis:2147483647
INFO (ConnectionPool.java:75) - getPooledMaxLifeTimeMillis:2147483647
INFO (ConnectionPool.java:76) - isOpen:true
INFO (ConnectionPool.java:78) - /127.0.0.1:3608 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C1] creationTime=2011.12.06 14:26:37, ageMillis=5141, elapsedLastUsageMillis=5031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3613 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C2] creationTime=2011.12.06 14:26:38, ageMillis=4031, elapsedLastUsageMillis=4031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3614 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C3] creationTime=2011.12.06 14:26:39, ageMillis=3031, elapsedLastUsageMillis=3031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3615 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C4] creationTime=2011.12.06 14:26:40, ageMillis=2031, elapsedLastUsageMillis=2031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3616 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C5] creationTime=2011.12.06 14:26:41, ageMillis=1031, elapsedLastUsageMillis=1031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:81) - --------------------

Why 5 connections has been created? In my opinion, only one connection should be created, because :
 
  for (int i = 0; i < 5; i++){
  sendMessage("127.0.0.1",8889,"Send messsage to server.");
  Thread.sleep(1000);
  }

同样的address and port, and "bc = pool.getBlockingConnection(host, port);" means "getting a pool connection for the given address. If no free connection is in the pool, a new one will be created. "

但是从第二次循环开始,该地址和端口已经有一个连接了,就不应该建立连接了.

2 楼 tony_zq 2011-12-08   还有一个问题,就是如果设置:
bc.setIdleTimeoutMillis(1000); //sets the idle timeout in millis,在调用close方法后开始算起的

那么1秒后,连接不是idle,而是Destroyed

INFO (ConnectionPool.java:67) - getNumIdle:0
INFO (ConnectionPool.java:68) - getNumActive:0
INFO (ConnectionPool.java:69) - getNumCreated:5
INFO (ConnectionPool.java:70) - getNumDestroyed:5

bc.close(); 的意思是// always close the connection! (the connection will be returned into the connection pool),同时设置了bc.setIdleTimeoutMillis(1000);按理结果应该是:
INFO (ConnectionPool.java:67) - getNumIdle:5
INFO (ConnectionPool.java:68) - getNumActive:0
INFO (ConnectionPool.java:69) - getNumCreated:5
INFO (ConnectionPool.java:70) - getNumDestroyed:0

问题出在哪里??????

热点排行