loadrunner socket协议问题归纳(4)---buffer接收变长和定长的数据

时间:2021-09-20 07:11:15

测试场景:聊天系统

用户登录后,要先向服务器发送用户名,然后可以发送聊天信息,同时也可以接受聊天信息。

如果接受的字符为定长时,可以设定接受长度。recv buf2 66

#include "lrs.h"
Action()
{
    int i = 1;
    char *recvbuf;  
    int recvlen=0;

lr_start_transaction("Transform");

//创建连接
lrs_create_socket("socket0", "TCP", "RemoteHost=172.30.22.77:8480", LrsLastArg);
//发送用户名
lrs_send("socket0", "buf0", LrsLastArg);
//发送聊天信息
lrs_send("socket0", "buf1", LrsLastArg);
/*
do{
lrs_send("socket0", "buf1", LrsLastArg);
i++;
}while(i<5);

*/
//接受聊天信息
lrs_receive("socket0", "buf2", LrsLastArg);
    lrs_get_last_received_buffer("socket0",&recvbuf,&recvlen);
    lr_output_message("Received:\n%s",lr_eval_string(recvbuf));
lr_output_message("recvlen=%d",recvlen);
//断开连接
lrs_close_socket("socket0");
lr_end_transaction("Transform", LR_AUTO);

return 0;
}

data.ws

;WSRData 2 1

send buf0 5
"Kevin\n"

send buf1 5
"Hello\n"

recv buf2 66

-1

运行结果:

Starting action Action.
Action.c(10): Notify: Transaction "Transform" started.
Action.c(20): lrs_create_socket(socket0, TCP, ...)
Action.c(21): lrs_send(socket0, buf0)
Action.c(22): lrs_send(socket0, buf1)
Action.c(28): lrs_receive(socket0, buf2)
Action.c(29): lrs_get_last_received_buffer(socket0, buf_p, size_p)
Action.c(30): Received:
your name is:
Kevin[896565348] come
Kevin[896565348] said :Hello
Action.c(31): recvlen=66
Action.c(33): lrs_close_socket(socket0)
Action.c(34): Notify: Transaction "Transform" ended with "Pass" status (Duration: 0.0289).

如果接受的消息长度为不定长的,可以使用*来代替长度。

将发送的数据使用参数化。

;WSRData 2 1

send buf0 5
"Kevin\n"

send buf1 *
"<SendMsg>"

recv buf2 *