1、lrs_create_socket("socket0", "TCP", "RemoteHost=127.0.0.1:8888", LrsLastArg),创建socket连接
2、lrs_send("socket0", "buf0", LrsLastArg),发送buf中的数据
3、lrs_receive("socket0", "buf1", LrsLastArg),接收服务器返回的数据,并保存到一个buf1中
4、lrs_save_param("socket0",NULL,"RecivedData",0,-1),获取服务器返回的数据,并保存到一个参数中RecivedData,最后两个参数,偏移量和length
5、lrs_save_searched_string("socket0",NULL,"RecivedData","LB/BIN=|","RB/BIN=|",1,0,-1),获取服务器返回的数据,并指定了左右边界,过滤出想要的数据,保存到一个参数中,最后三个参数分别为,取值序号、偏移量、保存长度
6、lrs_close_socket("socket0"),关闭链接 设置超时 lrs_set_recv_timeout2 当定义的buf长度与实际不匹配时,可以将接受数据的时间设置为0,避免等待的情况。 send buf0 10 "123" recv buf1 19
自编socket 接口源代码
1 import java.io.InputStream; 2 import java.io.OutputStream; 3 import java.net.ServerSocket; 4 import java.net.Socket; 5 6 public class TcpServer { 7 8 public static void main(String[] args) { 9 try { 10 ServerSocket serverSocket = new ServerSocket(8888); 11 System.out.println("server start..."); 12 while (true){ 13 Socket socket = serverSocket.accept(); 14 InputStream inputStream = socket.getInputStream(); 15 byte[] by = new byte[1024]; 16 int len = inputStream.read(by); 17 System.out.println("Msg from "+socket.getInetAddress().getHostAddress()+":"+new String(by,0,len)); 18 OutputStream outputStream = socket.getOutputStream(); 19 outputStream.write("|0|SUCCESS|1000913|".getBytes()); 20 outputStream.flush(); 21 } 22 // socket.close(); 23 }catch (Exception e){ 24 e.printStackTrace(); 25 } 26 } 27 }
测试脚本源码
1 #include "lrs.h" 2 3 4 Action() 5 { 6 //开始事务 7 lr_start_transaction("tcp"); 8 //建立连接 9 lrs_create_socket("socket0", "TCP", "RemoteHost=127.0.0.1:8888", LrsLastArg); 10 //开始发送 buf0 数据 11 lrs_send("socket0", "buf0", LrsLastArg); 12 //接收并保持一个buf1 13 lrs_receive("socket0", "buf1", LrsLastArg); 14 //相当于提去SUCCESS这个值 ,存惨 15 lrs_save_param("socket0",NULL,"RecivedData",3,7); 16 17 //事务判断 18 //注意 <> 使用参数是尖括号 19 if(strcmp(lr_eval_string("<RecivedData>"),"SUCCESS")==0){ 20 21 //结束事务 22 lr_end_transaction("tcp", LR_PASS); 23 24 }else{ 25 26 lr_end_transaction("tcp", LR_FAIL); 27 28 } 29 30 31 return 0; 32 }
1 Virtual User Script started at : 2019-04-03 20:26:48 2 Starting action vuser_init. 3 vuser_init.c(6): lrs_startup(257) 4 Ending action vuser_init. 5 Running Vuser... 6 Starting iteration 1. 7 Starting action Action. 8 Action.c(9): Notify: Transaction "tcp" started. 9 Action.c(11): lrs_create_socket(socket0, TCP, ...) 10 Action.c(13): lrs_send(socket0, buf0) 11 Action.c(13): Notify: Parameter Substitution: parameter "num" = "1740" 12 Action.c(15): lrs_receive(socket0, buf1) 13 ================================RECEIVED BUFFER================================ 14 "|0|SUCCESS|1000913|" 15 =============================================================================== 16 Action.c(15): callRecv:19 bytes were received 17 Action.c(19): lrs_save_param(socket0, null, RecivedData, 3, 7) 18 Action.c(19): Notify: Saving Parameter "RecivedData = SUCCESS". 19 Action.c(26): Notify: Parameter Substitution: parameter "RecivedData" = "SUCCESS" 20 Action.c(28): Notify: Transaction "tcp" ended with "Pass" status (Duration: 0.0316). 21 Action.c(36): lrs_close_socket(socket0) 22 Ending action Action. 23 Ending iteration 1. 24 Ending Vuser... 25 Starting action vuser_end. 26 vuser_end.c(6): lrs_cleanup() 27 Ending action vuser_end. 28 Vuser Terminated.
data.ws需要添加这几行
send buf0 10
"<num>"
recv buf1 19