qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          基于Loadrunner平臺Socket協議的JavaVuser(多線程)

          /* 
               * LoadRunner Java script. (Build: 15) 
               * Script Description:  
               * 
               * 作者:谷博濤 
               * 制作時間:2012-1-18 
               * E-mail:gubotao@foxmail.com 
               * Loadrunner:11.00 
               *  
               * 內容概要: 
               * 模擬基于Socket協議的即時消息系統中的客戶端行為LR腳本, 
               * 為模擬真實IM客戶端,接收消息和發送消息分兩個線程工作。 
               *  
               */  
                
              import lrapi.lr;  
              import java.io.IOException;  
              import java.io.InputStream;  
              import java.io.OutputStream;  
              import java.net.Socket;  
              import java.net.UnknownHostException;  
              import java.util.Iterator;  
              import java.util.concurrent.atomic.AtomicBoolean;  
                
              public class Actions  
              {  
                  // 客戶端  
                  private Socket client;  
                  // 房間號  
                  private String roomId;  
                  // 用戶ID  
                  private String id;  
                  // 輸出流  
                  private OutputStream out;  
                  // 輸入流  
                  private InputStream in;  
                  // 連接標志  
                  //private boolean connFlag = false;  
                  private  AtomicBoolean connFlag =new AtomicBoolean(false);  
                
                  // 客戶端是否結束標志  
                  //private boolean endFlag = true;  
                  private  AtomicBoolean endFlag =new AtomicBoolean(true);  
                
                
                  public int init() throws Throwable {  
                      connect();   
                  //lr.think_time(10);  
                      return 0;  
                  }//end of init  
                
                
                  public int action() throws Throwable {  
                      sendAction();  
                      return 0;  
                  }//end of action  
                
                
                  public int end() throws Throwable {  
                      sendEnd();  
                      return 0;  
                  }//end of end  
                
              //====主題代碼==================//  
                
                  //連接服務器  
                  private void connect() {  
                      try {  
                              client = new Socket("127.0.0.1", 5222);  
                              System.out.println("connect success!!!");  
                              out = client.getOutputStream();  
                              in = client.getInputStream();  
                              receiveAction();  
                              login();  
                      } catch (UnknownHostException e) {  
                              System.out.println("UnknownHostException=" + e);  
                              e.printStackTrace();  
                      } catch (IOException e) {  
                              System.out.println("IOException=" + e);  
                              e.printStackTrace();  
                      }  
                  }  
                
                  //登錄服務器  
                  private void login() {  
                      String loginMsg = "<msg type=\"login\" channel=\"CCTV1\"></msg>";  
                      sendMsg(loginMsg);  
                  }  
                
                  //啟動接收線程  
                  private void receiveAction() {  
                  new ReceiveThread().start();  
                  }  
                
                  //得到房間號碼和用戶ID  
                  private void getEleVal(String msg) {  
                  int index =0;  
                
                  index = msg.indexOf("to");  
                  msg = msg.substring(index + 4, msg.length());  
                  index = msg.indexOf("'");  
                  id = msg.substring(0, index);  
                  System.out.println(roomId);  
                
                  index = msg.indexOf("roomId");  
                  msg = msg.substring(index + 8, msg.length());  
                  index = msg.indexOf("'");  
                  roomId = msg.substring(0, index);  
                  System.out.println(id);  
                
                  connFlag.set(true);  
                  }  
                
                  //發送消息  
                  private void sendAction() {  
                  if(connFlag.get()){  
                      System.out.println("發送消息----->");  
                      String msg = "<msg type=\"groupchat\" channel=\"CCTV1\" from=\"" + id  
                      + "\" to=\"" + roomId + "\"><body>test</body></msg>";  
                    
                      sendMsg(msg);  
                  }  
                  }  
                
                  //調用寫入流方法  
                  private void sendMsg(String msg) {  
                  //new SendThread(msg).start();  
                  writeMsg(msg);  
                  }  
                
                  //將消息寫入流  
                  private void writeMsg(String msg) {  
                  try {  
                      if (out != null) {  
                      out.write(msg.getBytes("UTF-8"));  
                      out.flush();  
                      }  
                  } catch (Exception e) {  
                      System.out.println("Exception=" + e);  
                  }  
                  }  
                
                  //關閉客戶端  
                  private void sendEnd() {  
                  endFlag.set(false);  
                  try {  
                      client.close();  
                  } catch (IOException e) {  
                      // TODO Auto-generated catch block  
                      e.printStackTrace();  
                  }  
                  }  
                  /** 
                   * 接收消息線程類 
                   *  
                   * @author Administrator 
                   *  
                   */  
                  private class ReceiveThread extends Thread {  
                  @Override  
                  public void run() {  
                      while (endFlag.get()) {// 循環接收消息  
                          try {  
                          int len = in.available();  
                
                          if(len>0){  
                              System.out.println(len);  
                              byte[] bytes = new byte[len];  
                              in.read(bytes);  
                              String result = new String(bytes);  
                              System.out.println("接收到的消息:" + result);  
                
                              if(result != null && !"".equals(result)&& result.contains("res_bind")){  
                              getEleVal(result);  
                              }  
                          }  
                          } catch (Exception e) {  
                          // TODO: handle exception  
                          }  
                      }  
                  }  
                  }  
                
                
              //======發送消息線程類(本代碼未用到)=========  
                  private class SendThread extends Thread {  
                  private String msg;  
                    
                  public SendThread(String msg) {  
                      this.msg = msg;  
                  }  
                    
                  @Override  
                  public void run() {  
                      if (connFlag.get()) {  
                      writeMsg(msg);  
                      }  
                  }  
                  }  
                
              }//end for class Actions  

          posted on 2012-06-19 11:27 順其自然EVO 閱讀(1160) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2012年6月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 成都市| 惠来县| 常熟市| 屏南县| 新安县| 晋州市| 大竹县| 山阴县| 博爱县| 六盘水市| 高雄县| 临武县| 灵宝市| 重庆市| 福泉市| 凤凰县| 娄烦县| 肥乡县| 旺苍县| 牡丹江市| 周宁县| 沙河市| 江油市| 杨浦区| 香格里拉县| 聂拉木县| 莲花县| 隆德县| 红安县| 中西区| 江达县| 财经| 上林县| 霍林郭勒市| 尼木县| 石河子市| 荥阳市| 金阳县| 蓝田县| 陆良县| 库车县|