LoadRunner Socket Scripting Tips

Source: Internet
Author: User

Writing Steps 

1. Establish a connection to the server

Rc=lrs_create_socket ("Socket0", "TCP", "localhost=0", "remotehost=127.0.0.1:8808", Lrslastarg);



Service-side address and port number



Whether the connection was successful.



Rc=0 indicates success in establishing connection



2. Sending messages and receiving messages



Lrs_send ("Socket0", "buf0", Lrslastarg);



Send data in buf0 by Socket0 Channel



Lrs_receive ("Socket0", "Buf1", Lrslastarg);



Store the data returned by the socket channel in BUF1 


3. Close the connection



Lrs_close_socket ("Socket0");



Close Socket0 Channel



Full Script reference:



Action ()



{



Char*recvbuf;



intrecvlen=0;



intrc;



lr_start_transaction ("Trans_socket");//Transactions



lrs_set_recv_timeout (60,0);//Receive time-out



lr_start_transaction ("Conn_socket");



rc= lrs_create_socket ("Socket0", "TCP", "localhost=0", "remotehost=127.0.0.1:8808", Lrslastarg); RemoteHost Place the server IP where the program is being tested



lr_output_message ("rc=%d", RC);



if (rc! = 0) {



lr_end_transaction ("Conn_socket", lr_fail);



lr_end_transaction ("Trans_socket", lr_fail);



Return0;



}else{



lr_end_transaction ("Conn_socket", Lr_pass);} Determine if the socket is linked to a successful transaction



lr_rendezvous ("meeting point");



lrs_send ("Socket0", "buf0", lrslastarg);



lrs_receive ("Socket0", "Buf1", lrslastarg);



Lrs_get_last_received_buffer ("Socket0", &recvbuf,&recvlen);//Get return content and return length



//Determine if the message length is correct



if (recvlen==304)



lr_end_transaction ("Trans_socket", lr_pass);



Else



lr_end_transaction ("Trans_socket", Lr_fail); Determine whether the length of the returned information is correct, and the length of the expected return information at Recvlen



lrs_close_socket ("Socket0");



Return0;



The association function introduces lrs_save_searched_string ();



Searches for the occurrences of a string in a static or received buffer, saves the portion of the buffer that appears in the string to the parameter in 

instance 1:



The original data is: "0| General Command issued a successful |10000031|."



Lrs_save_searched_string ("Socke0", "Buf1", "WTBH", "lb/bin=|", "rb/bin=|", 2,0,-1);



Output result: "WTBH = 10000013". At the 2nd occurrence of the left and right borders, get all the values inside and around the boundary.



instance 2:



The original data is: "0| General Command issued a successful |10000031|."



Lrs_save_searched_string ("Socke0", "Buf1", "WTBH", "lb/bin=|", "rb/bin=|", 1,0,-1);



Output: "WTBH = Common Command issued successfully", in the left and right edge of the 1th occurrence of the boundary, get all the values inside and outside the boundary 

Instance 3:



The original data is: "0| General Command issued successful |10000031|"



Lrs_save_searched_string ("Socke0", "Buf1", "WTBH", "lb/bin=|", "rb/bin=|", 1,4,-1);



• Output: "WTBH = command release succeeded", and at the 1th occurrence of the left and right borders, get the data after the 4th character from the beginning of the left boundary. 

2.lrs_save_param ();



Saves a static or received buffer (or portion of the buffer) to an instance in a parameter :



Lrs_save_param ("Socket0", "Buf1", "WTBH", 34, 8);






Takes 8 characters consecutively from the 34th character in a packet.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.