java多线程loadrunner_基于Loadrunner平台Socket协议的JavaVuser(多线程)

/*

* LoadRunner Java script. (Build: 15)

* Script Description:

*

* 作者:谷博涛

* 制作时间:2012-1-18

* E-mail:captain_gbt@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 = "";

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 = "

+ "\" to=\"" + roomId + "\">

test";

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

谷博涛

二零一二年 农历腊月廿五 下午

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值