`
maosheng
  • 浏览: 551491 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

基于LoadRunner平台Socket协议的JavaVuser

    博客分类:
  • Test
阅读更多
/*
* LoadRunner Java script.
* Loadrunner:11.00.0.0
*
* 脚本描述:
* 模拟基于Socket协议的即时消息系统中的客户端行为LR脚本,
* 为模拟真实POS终端,接收消息和发送消息分两个线程工作。
*
* 作者:maosheng
* 制作时间:2013-1-11

*/ 
 
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 OutputStream out; 
    // 输入流 
    private InputStream in; 
    // 客户端是否连接标志 
    //private boolean connFlag = false; 
    private  AtomicBoolean connFlag =new AtomicBoolean(false);
 
 
    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("172.16.4.168", 8888); 
                System.out.println("connect success!!!");
connFlag.set(true);
                out = client.getOutputStream(); 
                in = client.getInputStream();
        } catch (UnknownHostException e) { 
                System.out.println("UnknownHostException=" + e); 
                e.printStackTrace(); 
        } catch (IOException e) { 
                System.out.println("IOException=" + e); 
                e.printStackTrace(); 
        } 
    } 

 
    //发送消息 
    private void sendAction() { 
if(connFlag.get()){  
   
            //收到报文:[0001 1 0 31]0001123456789          e590986a
            //发送报文:
            byte[] byteMsg=new byte[]{
                    Byte.parseByte("0"),
    Byte.parseByte("1"),
    Byte.parseByte("0"),
    Byte.parseByte("1"),
    Byte.parseByte("0"),
    Byte.parseByte("0"),
    Byte.parseByte("31"),
    Byte.parseByte("48"),
    Byte.parseByte("48"),
    Byte.parseByte("48"),
    Byte.parseByte("49"),
    Byte.parseByte("49"),
    Byte.parseByte("50"),
    Byte.parseByte("51"),
    Byte.parseByte("52"),
    Byte.parseByte("53"),
    Byte.parseByte("54"),
    Byte.parseByte("55"),
    Byte.parseByte("56"),
    Byte.parseByte("57"),
    Byte.parseByte("32"),
    Byte.parseByte("32"),
    Byte.parseByte("32"),
    Byte.parseByte("32"),
    Byte.parseByte("32"),
    Byte.parseByte("32"),
    Byte.parseByte("32"),
    Byte.parseByte("32"),
    Byte.parseByte("32"),
    Byte.parseByte("32"),
    Byte.parseByte("101"),
    Byte.parseByte("53"),
    Byte.parseByte("57"),
    Byte.parseByte("48"),
    Byte.parseByte("57"),
    Byte.parseByte("56"),
    Byte.parseByte("54"),
    Byte.parseByte("97")
};

            String msg = new String(byteMsg);

            System.out.println("发送的消息:"+msg);
    sendMsg(msg);
    receiveMsg();

    } 
   
    //关闭客户端 
    private void sendEnd() { 
connFlag.set(false); 
try { 
    client.close(); 
            System.out.println("connect closed!!!");
} catch (IOException e) { 
    e.printStackTrace(); 

    } 
 
    //调用写入流方法 
    private void sendMsg(String msg) { 
//new SendThread(msg).start(); 
writeMsg(msg); 
    } 
 
    //将消息写入流 
    private void writeMsg(String msg) { 
    try { 
        if (out != null) { 
        out.write(msg.getBytes("GBK")); 
        out.flush(); 
        } 
    } catch (Exception e) { 
        System.out.println("Exception=" + e); 
    } 
    } 
   
     //接收消息
    private void receiveMsg() { 
      //new ReceiveThread().start(); 
getMsg();
    } 
 
    //得到返回结果 
    private void getMsg() {
       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);
if(result != null && !"".equals(result)){ 
   System.out.println("接收到的消息:" + result); 

}
            } catch (Exception e) { 
               e.printStackTrace();  
            }   
    } 
 
/******************************************************************/
    /**
     * 接收消息线程类
     * 
     * @author maosheng
     * 
     */ 
    private class ReceiveThread extends Thread { 
    @Override 
    public void run() { 
        while(connFlag.get()) {
    getMsg();
            try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
        } 
      } 
    } 
 
 
   /**
     * 发送消息线程类
     * 
     * @author maosheng
     * 
     */  
    private class SendThread extends Thread {
   
    private String msg; 
     
    public SendThread(String msg) { 
        this.msg = msg; 
    } 
     
    @Override 
    public void run() { 
        while (connFlag.get()) { 
     writeMsg(msg);
             try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
        } 
    } 
  } 
 
}//end for class Actions 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics