LSP就是lrs_save_param()函式
LSSS就是lrs_save_searched_string()函式
一下我們用一個例子去說明他們的使用.
C/S機制和B/S不一樣,特別是有一個data.ws
裡面的資料就是一些向服務端發的資料包和服務端返回的資料包
以下就是我錄製完以後的data.ws內容
;WSRData 2 1
send buf0 55
"4##SELECT agent_name FROM AGENTS ORDER BY agent_name###"
recv buf1 55
"0##Alex#Amanda#Debby#Julia#Mary#Robert#Sharon#Suzan###"
"\x00"
send buf2 68
"2##1## SELECT DISTINCT departure FROM Flights ORDER BY departure ###"
recv buf3 56
"0##Denver#Los Angeles#Portland#San Francisco#Seattle###"
"\x00"
send buf4 298
"2##0##SELECT departure, flight_number, departure_initials, day_of_week, ar"
"rival_initials, arrival, departure_time, arrival_time, airlines, seats_ava"
"ilable, ticket_price, mileage FROM Flights WHERE arrival = 'Portland' A"
"ND departure = 'Denver' AND day_of_week = 'Friday'ORDER BY flight_number #"
"##"
recv buf5 128
"0##5787;250;5587;03:11 PM;DEN;Friday;POR;04:52 PM;NW;164;Denver#6242;250;6"
"042;08:00 AM;DEN;Friday;POR;11:00 AM;AA;177;Denver###"
"\x00"
send buf6 82
"11##UPDATE Counters SET counter_value=counter_value+1 WHERE table_name='OR"
"DERS'###"
recv buf7 8
"0##1###"
"\x00"
send buf8 67
"12##SELECT counter_value FROM Counters WHERE table_name='ORDERS'###"
recv buf9 10
"0##101###"
"\x00"
send buf10 72
"12##SELECT customer_no FROM Customers WHERE customer_name='<Customer>'###"
recv buf11 9
"0##-1###"
"\x00"
send buf12 85
"11##UPDATE Counters SET counter_value=counter_value+1 WHERE table_name='CU"
"STOMERS'###"
recv buf13 8
"0##1###"
"\x00"
send buf14 70
"12##SELECT counter_value FROM Counters WHERE table_name='CUSTOMERS'###"
recv buf15 9
"0##31###"
"\x00"
send buf16 81
"11##INSERT INTO Customers (customer_name,customer_no) VALUES ('<Customer>',"
" <userid>)###"
recv buf17 8
"0##1###"
"\x00"
send buf18 58
"12##SELECT agent_no FROM Agents WHERE agent_name='<agent>'###"
recv buf19 8
"0##4###"
"\x00"
send buf20 195
"11##INSERT INTO Orders (order_number,agent_no,customer_no,flight_number,de"
"parture_date,tickets_ordered,class,send_signature_with_order) VALUES (<orderno>,"
" <agentid>, <userid>, 6242, {d '2011-11-11'}, 1, '3', 'N')###"
recv buf21 8
"0##1###"
"\x00"
send buf22 13
"11##COMMIT###"
recv buf23 8
"0##0###"
"\x00"
-1
下面就是我們的ACTION裡面的指令碼了
#include "lrs.h"
Action()
{
int random;
lrs_create_socket("socket0", "TCP", "RemoteHost=172.16.2.9:3456", LrsLastArg);
lrs_send("socket0", "buf0", LrsLastArg); //返回agent name
lrs_receive("socket0", "buf1", LrsLastArg);
srand(time(NULL));
random=rand() % 8 + 2;
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
lr_think_time(12);
lrs_send("socket0", "buf2", LrsLastArg); //返回航班起始地和目的地
lrs_receive("socket0", "buf3", LrsLastArg);
lr_think_time(13);
lrs_send("socket0", "buf4", LrsLastArg); //查詢航班資訊
lrs_receive("socket0", "buf5", LrsLastArg);
lr_think_time(18);
lrs_send("socket0", "buf6", LrsLastArg); //生成訂單號
lrs_receive("socket0", "buf7", LrsLastArg);
lrs_send("socket0", "buf8", LrsLastArg); //返回訂單號碼
lrs_receive("socket0", "buf9", LrsLastArg);
lrs_save_param("socket0", NULL, "orderno", 3, 3);//利用關聯返回訂單號
lrs_send("socket0", "buf10", LrsLastArg); //查詢是否是新使用者,新使用者返回-1,否則返回客戶編號
lrs_receive("socket0", "buf11", LrsLastArg);
lrs_save_param("socket0", NULL, "userid", 3, 2);
if (strcmp(lr_eval_string("<userid>"),"-1")==0)
{
lrs_send("socket0", "buf12", LrsLastArg);//生成客戶編號
lrs_receive("socket0", "buf13", LrsLastArg);
lrs_send("socket0", "buf14", LrsLastArg);//返回客戶編號
lrs_receive("socket0", "buf15", LrsLastArg);
lrs_save_param("socket0", NULL, "userid", 3, 2);
lrs_send("socket0", "buf16", LrsLastArg);//插入客戶記錄
lrs_receive("socket0", "buf17", LrsLastArg);
};
lrs_send("socket0", "buf18", LrsLastArg); //返回agent id
lrs_receive("socket0", "buf19", LrsLastArg);
lrs_save_param("socket0", NULL, "agentid", 3, 1);
lrs_send("socket0", "buf20", LrsLastArg); //插入訂單記錄
lrs_receive("socket0", "buf21", LrsLastArg);
lrs_send("socket0", "buf22", LrsLastArg); //提交
lrs_receive("socket0", "buf23", LrsLastArg);
lr_think_time(7);
return 0;
}
srand(time(NULL));
random=rand() % 8 + 2;
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
我們從這段程式碼開始分析。
srand(time(NULL));
random=rand() % 9 + 2;
這個隨機數的作用是為了
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
函式從
recv buf1 55
"0##Alex#Amanda#Debby#Julia#Mary#Robert#Sharon#Suzan###"
"\x00"
中隨機獲得一個關聯資料,為什麼是資料說的範圍是2-9呢,細看一下
##Alex#Amanda#Debby#Julia#Mary#Robert#Sharon#Suzan###
就知道以左邊界和右邊界為#的是有11對D,不過只有第二開始到第九個才是對我們有用的。
所以我們這樣取值。從上面我們也可以看出WINSOCKET協議裡面使用的關聯和HTTP/HTML協議裡面的關聯函式是有區別的,區別是http的協議裡面關聯函式的位置一定要在開啟的頁面的前面,在Winsocket裡面是在後面。
所以執行完以下指令碼
srand(time(NULL));
random=rand() % 8 + 2;
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
agent就會隨機得到Alex、Amanda、Debby、Julia、Mary、Robert、Sharon、Suzan其中的一個。
我再來分析
lrs_save_param("socket0", NULL, "orderno", 3, 3);
這段程式碼是data.ws包裡面提取所以要的資料
recv buf9 10
"0##101###"
"\x00"
從以下的範例
lrs_save_param( char *s_desc,char *buf_desc, char *param_name, intoffset, intparam_len);
可以看出
lrs_save_param("socket0", NULL, "orderno", 3, 3);
是從"0##101###"的第3位開始取值,取3位,所以
Orderon=101
再來說一說這兩個函式的一個大家不太瞭解的地方,然後就結束本章。
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
lrs_save_param("socket0", NULL, "orderno", 3, 3);
其中我們都發現有一個NULL,NULL的作用就是在剛從服務端裡面的資料中取值,
lrs_send("socket0", "buf8", LrsLastArg); //返回訂單號碼
lrs_receive("socket0", "buf9", LrsLastArg);
lrs_save_param("socket0", NULL, "orderno", 3, 3);
如果寫NULL,我們要從buf9中取值的話,關聯函式就得在
lrs_receive("socket0", "buf9", LrsLastArg);
的後面
如果
lrs_receive("socket0", "buf10", LrsLastArg);
lrs_save_param("socket0", NULL, "orderno", 3, 3);
就是在duf10中取值。
還有一種情況如果在
lrs_receive("socket0", "buf10", LrsLastArg);
後面取duf9的值就必須這麼寫
lrs_save_param("socket0","buf9", "orderno", 3, 3);