Loadrunner socket script writing: [loadrunner recording script will not be used, and I don’t understand it very well, let’s write it]
1. Create a windows socket script, as shown in the figure
2. Script in Action:
#include "lrs.h"
Action()
{
char *noticeReply;
char *result;
int rc=0;
int receive_code;
//建立socket
rc=lrs_create_socket("socket0","TCP","RemoteHost=127.0.0.1:8082",LrsLastArg);
if (0==rc) {
lr_output_message("Socket was successfully created ");
}
else
{
lr_output_message("An error occurred while creating the socket, Error Code: %d",rc);
}
//Start transaction
lr_start_transaction("socket_trans");
//Send data
lrs_send("socket0", "buf0", LrsLastArg);
//end sending
//lrs_disable_socket("socket0", DISABLE_SEND);
//Receive the data sent back by the server
receive_code=lrs_receive("socket0", "buf1", LrsLastArg);
//Receive the return message in real time, intercept the return message, 4 characters after the 64th bit in the string, four 0000
lrs_save_param("socket0",NULL,"noticeReply",61,4);
// Deal with Chinese garbled characters
lr_convert_string_encoding(lr_eval_string("<noticeReply>"),"utf-8",NULL,"result");
//Print the message after processing Chinese garbled characters
lr_output_message("correlation=%s", lr_eval_string("<result>"));
//The four 0000s in the message I returned are success, so judge like this
if (strcmp(lr_eval_string("<result>"),"0000")==0){ lr_end_transaction("socket_trans",LR_PASS); } else { lr_end_transaction("socket_trans",LR_FAIL); } //Close the open socket lrs_close_socket("socket0");
return 0;
}
3. For the example in the data.ws file, change it according to your own situation
;WSRData 2 1
send buf0 40
"00000032{'name':'zyw','address':'12345'}"
recv buf1 256
-1
4. Problems encountered.
①. After receiving the returned data, Chinese characters are garbled.
lr_convert_string_encoding(lr_eval_string("<noticeReply>"),"utf-8",NULL,"result");
②. Acquisition of the sign to judge whether the script thing is successful, Baidu downloads this method, understand and change it
lrs_save_param("socket0",NULL,"noticeReply",61,4);
③. Modify the message response success sign
strcmp(lr_eval_string("<result>"),"0000")==0