Socket script writing method LR 2 (read from the file packet)

 

  Previously, to share a script written agreement socket LoadRunner basic methods and rules , under today to share how from a local file, read the contents, and as a message sent to the server; the method also encountered at work one difficulty, think this way, several parameterized transaction message, read different file through an iterative, that can send different messages of the transaction.

1. int default and end with the same, do not explain;

2. Action script writing

#include "lrs.h" 


the Action () 
{ 

        int RC, RV; // save the connection is successful return value 
	char * recvBuf; // save the contents of the received data, 
	int recvLen; // save the received data size 


    int count, total = 0 ; 
    char Buffer [1000]; 
    Long file_stream; 
    char * filename = "E: /LR_date/ZHQZ/LR_scripts/baowen.txt"; 
    IF ((= file_stream the fopen (filename, "RB")) == NULL) { 
        lr_error_message ( "Can Not Open% S", filename); 
        return -1; 
    } 
    // End of File an until the Read 
    the while (! feof (file_stream)) { 
        // the Read 1000 bytes Maintaining the while running COUNT A  
        COUNT = fread (Buffer, the sizeof (char ), 1000, file_stream);
        lr_output_message ( "% D bytes Read", count);
//		lr_output_message ("读取文件的buffer内容:\n %s", buffer);
        if (ferror(file_stream)) { /* Check for file I/O errors */ 
            lr_output_message ("Error reading file %s", filename); 
            break; 
        } 
        total += count; // Add up actual bytes read 
    } 
    // Display final total 
    lr_output_message ("Total number of bytes read = %d", total ); 
    // Close the file stream 
    if (fclose(file_stream)) 
		lr_error_message ("Error closing file %s", filename); 


//	memcpy(buffer+271,lr_eval_string("<custID>"),8);

	lrs_save_param_ex("socket0", "user", buffer, 0, strlen(buffer),NULL, "paraBuf ");

	lrs_create_socket = RC ( "SOCKET0", "the TCP", "the RemoteHost = 188.177.155.233: 7700", LrsLastArg); 


	lr_start_transaction ( "Test"); // start transaction 

	// socket send request message (packet content in data. in ws) 
	lrs_send ( "SOCKET0", "of buf1", LrsLastArg); // fetch buffer and transmitting the packets buf0 


	rv = lrs_receive ( "socket0", "buf2", LrsLastArg); // receiving a response packet 


	// Get last received buffer and size 
	lrs_get_last_received_buffer ( "SOCKET0", & recvbuf, & recvLen); 


	/ * set the check point, to verify whether the return data is successful, the judgment, * / according to the respective trading situation 

	iF (recvLen>. 3) 
	{ 
		the lr_end_transaction ( "test", the PASS); 
	} 
	the else 
	{ 
		the lr_end_transaction ( "test", FAIL);  
		lr_error_message ( "test failed error message:! [% S]", recvbuf); 
	}

	lrs_free_buffer (recvbuf);// release recvBuf memory space, otherwise it will lead to memory leaks 


	lrs_close_socket ( "SOCKET0"); 

	
    return 0; 
}

  The upper part is designated from the script at a certain path, to read the contents of the file. Some people say that if I want to file a field parameterize how to do; then we can use this function memcpy be parameterized, such as for example: memcpy (buffer + 271, lr_eval_string ( "<custID>"), 8); // this is a parametric value by calling the read packet custID pool of 272 to 279, a total of eight bytes of parameters.

Written 3. data.ws

;WSRData 2 1

send  buf1 683
"<paraBuf>"

recv buf2 350
""
-1

   Wherein, in the Action packet it has been acquired, stored by the correlation function to the paraBuf lrs_save_param_ex parameter; In this case, we can call to, directly above the parameter name.

 

ENDing...

 

  

 

Guess you like

Origin www.cnblogs.com/zwh-Seeking/p/11119148.html