// ssl 加密导入略
/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description: dengdagui
*
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import lrapi.lr;
import java.util.Random;
import java.util.Calendar;
import java.net.*;
import javax.net.ssl.*;
import javax.net.*;
import java.io.*;
import lrapi.lr;
public class Actions
{
private Socket socket; // 连接套接口
private DataInputStream inputStream; // 套接口输入流
private DataOutputStream outputStream; // 套接口输出流
private StringBuffer responseBuffer; // 服务器端返回消息
private Random randomGen; // 定义一个随机数类
private int state; // 状态标示
private int room; // 用户所在房间号
private final int SERVER_NOT_CONNECT = 0; // 未连接服务器
private final int SERVER_CONNECT_OK = 1; // 连接服务器成功
/*
初始化
*/
public int init() throws Throwable {
state = SERVER_NOT_CONNECT;
socket = null;
inputStream = null;
outputStream = null;
responseBuffer = new StringBuffer();
randomGen = new Random();
randomGen.setSeed(Calendar.getInstance().getTimeInMillis());
return 0;
}//end of init
/*
向服务器发送socket连接请求
*/
public void connect(String host, int port)
{
// 已登陆退出
// closeSocket();
try
{
SocketAddress sockAddress = new InetSocketAddress(host, port);
socket = new Socket();
socket.setSoTimeout(500);
lr.start_transaction("Socket 连接");
socket.connect(sockAddress, 5000);
inputStream = new DataInputStream(socket.getInputStream());
outputStream = new DataOutputStream(socket.getOutputStream());
lr.end_transaction("Socket 连接", lr.PASS);
lr.message("[ connect ] Socket 连接成功:"+socket.toString());
state = SERVER_CONNECT_OK;
}
catch (Exception e)
{
lr.end_transaction("Socket 连接", lr.FAIL);
state = SERVER_NOT_CONNECT;
lr.error_message("[ connect ] Socket 连接失败");
e.printStackTrace();
}
}
/*
向服务器发送XML格式的数据
*/
public void sendXMLRequest(String content){
// content="REGISTER 213 1500004";
// send();
String request =content;
lr.message("[ sendXMLRequest ]:"+request);
// lr.start_transaction("进入房间");
try{
outputStream.writeBytes(request);
outputStream.writeByte(0);
outputStream.flush();
}catch(IOException e){
e.printStackTrace();
}
// lr.end_transaction("进入房间", lr.PASS);
}
/*
* 接受服务器端传来的XML格式数据
*/
public String receiveXMLResponse(){
String result=null;
responseBuffer.delete(0, responseBuffer.length());
// String result = "";
int i = 0;
try{
do{
int stream_length = inputStream.available();
if(stream_length > 0){
// inputStream中有数据
byte[] buf = new byte[stream_length];
inputStream.read(buf);
responseBuffer.append((new String(buf)).trim());
result = responseBuffer.toString();
// lr.message("[ receiveXMLResponse ] result long: "+stream_length);
// lr.message("[ receiveXMLResponse ] result:"+ result);
lr.message(result);
// lr.error_message("result long["+stream_length+"]"+" result["+result+"]");
// lr.end_transaction("进入房间", lr.PASS);
return result;
}else{
i++;
Thread.sleep(8000);
}
}while( i < 9 );
}catch(Exception e){
// lr.end_transaction("进入房间", lr.FAIL);
e.printStackTrace();
}
return result;
}
/*
断连接
*/
public void closeSocket(){
lr.start_transaction("Socket 断开连接");
try{
if(socket != null)
{
socket.close();
socket = null;
lr.message("[ closeSocket ]:Socket 断开连接成功");
}
lr.end_transaction("Socket 断开连接",lr.PASS);
}catch(Exception e){
lr.end_transaction("Socket 断开连接",lr.FAIL);
lr.error_message("[ closeSocket ]:Socket 断开连接失败");
e.printStackTrace();
}
}
public static void send(){
int port=8351;
try
{
String send_String="#shouji#{'body': {'username': 'cat', 'password': 'sha14811e$f145f30b3ffc0e833632f233caea16f40197a184', 'client_type': 1, 'client_info': 'WIN 11,1,102,55'}, 'tag': 'login', 'type': 'account'}";
SSLSocketFactory factory=(SSLSocketFactory)SSLSocketFactory.getDefault();
Socket s=factory.createSocket("127.285.168.176",port);
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
out.println(send_String);
System.out.println("aaaaaa");
out.close();
s.close();
}catch(Exception e)
{
System.out.println(e);
}
// return (null);
}
public int action() throws Throwable {
//
// connect("ip", 40887);
//
// /** --------进入房间--------**/
// lr.start_transaction("进入房间");
// sendXMLRequest("REGISTER ");
// lr.message("===============RECEIVED BUFFER==============");
// if(receiveXMLResponse()!=null){
// lr.message("============================================");
// lr.end_transaction("进入房间", lr.PASS);
// }else
// lr.end_transaction("进入房间", lr.FAIL);
//
//
// /** --------买入筹码--------**/
// lr.start_transaction("买入筹码");
// sendXMLRequest("SITDOWN 20");
// lr.message("===============RECEIVED BUFFER==============");
// if(receiveXMLResponse()!=null){
// lr.message("============================================");
//
// lr.end_transaction("买入筹码", lr.PASS);
// }else
// lr.end_transaction("买入筹码", lr.FAIL);
//
//
// /** --------退还筹码--------**/
// lr.start_transaction("退还筹码");
// sendXMLRequest("STANDUP ");
// lr.message("===============RECEIVED BUFFER==============");
// if(receiveXMLResponse()!=null){
// lr.message("============================================");
//
// lr.end_transaction("退还筹码", lr.PASS);
// }else
// lr.end_transaction("退还筹码", lr.FAIL);
//
//
// /** --------离开房间_座位--------**/
// lr.start_transaction("离开房间_座位");
// sendXMLRequest("UNREGISTER ");
// lr.message("===============RECEIVED BUFFER==============");
// if(receiveXMLResponse()!=null){
// lr.message("============================================");
//
// lr.end_transaction("离开房间_座位", lr.PASS);
// }else
// lr.end_transaction("离开房间_座位", lr.FAIL);
//
// closeSocket();
//
send();
// sendXMLRequest(null);
// if(receiveXMLResponse()!=null){
// lr.message("============================================");
// lr.end_transaction("进入房间", lr.PASS);
// }else
// lr.end_transaction("进入房间", lr.FAIL);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
阅读(989) | 评论(0) | 转发(0) |