loadrunner之socket协议脚本编写

1.java代码模拟服务器端程序:

package socket.com;
import java.io.*;   
import java.net.*;

public class Server {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		ServerSocket server=new ServerSocket(5678);   
		while(true){   
		Socket client=server.accept();   
		BufferedReader in=  
		new BufferedReader(new InputStreamReader(client.getInputStream()));   
		PrintWriter out=new PrintWriter(client.getOutputStream());   
		//while(true){   
		String str=in.readLine();   
		System.out.println(str);   
		out.println("hasreceive");   
		out.flush();   
		//if(str.equals("end"))   
		//break;   
		//}   
		client.close();   
		}   
	}

}

 2.loadrunner客户端代码:

#include "lrs.h"


Action()
{
	int rc;
	rc=lrs_create_socket("socket0","TCP","RemoteHost=127.0.0.1:5678",  LrsLastArg);
	if (rc==0) 
       lr_output_message("Socket  was successfully created "); 
	else 
       lr_output_message("An error occurred while creating the socket, Error Code: %d", rc);


    lr_start_transaction("CA");
	lrs_send("socket0", "buf0", LrsLastArg);   //往"socket0"发送"buf0"中的数据
    lrs_receive("socket0", "buf1", LrsLastArg);//将"socke0"中返回的数据存放到"buf1"中

    lrs_save_param_ex("socket0", "received", "buf1", 0, 10,"ebcdic", "res");
	lr_output_message(lr_eval_string("<res>")); 
	if(strcmp(lr_eval_string("<res>"),"hasreceive")==0){
		lr_end_transaction("CA", LR_PASS);
	}else{
		lr_end_transaction("CA", LR_FAIL);
	}

    //lrs_close_socket("socket0");
    

	return 0;
}

 data.ws文件内容:

;WSRData 2 1

    send buf0 5
    "hello\r\n"
    recv buf1 12
	"hasreceive"

-1

 

你可能感兴趣的:(loadrunner,socket协议,socket脚本)