前一段时间做一个转发工具压力测试,只是提供IP和端口,下面贴出来与大家分享,不足之处还请指正:
整个脚本写法很简单,大体来说,分三个步骤:
步骤1:建立到服务器端连接
rc = lrs_create_socket("socket0", "TCP", "LocalHost=0", "RemoteHost=127.0.0.1:8808", LrsLastArg);
注:rc=0则表示建立通讯成功
步骤2:发送报文和接收报文
lrs_send("socket0","buf0", LrsLastArg);//往socket0发送buf0的数据
lrs_receive ("socket0","buf1",LrsLastArg); //将socket0发送返回的数据存放到buf1中
步骤3:关闭连接
lrs_close_socket("socket0");//关闭
到此为止,socket通讯的单次的发送、接收报文基本没有什么问题了,完整源码如下:
#define _EOF ‘#‘ #include "lrs.h" Action() { char *recvbuf; int recvlen=0; int rc; lr_start_transaction("Trans_socket");//事务 lrs_set_recv_timeout (60,0);//接收超时时间 lr_start_transaction("Conn_socket"); //RemoteHost处填入被测程序所在服务器IP rc = lrs_create_socket("socket0", "TCP", "LocalHost=0", "RemoteHost=127.0.0.1:8808", LrsLastArg); lr_output_message("rc=%d",rc); if (rc != 0 ) { lr_end_transaction("Conn_socket", LR_FAIL); lr_end_transaction ("Trans_socket", LR_FAIL); return 0; } lr_end_transaction("Conn_socket", LR_PASS); //判断socket是否链接成功的事务 lr_rendezvous("集合点"); lrs_send("socket0","buf0", LrsLastArg); lrs_receive ("socket0","buf1",LrsLastArg); lrs_get_last_received_buffer ("socket0",&recvbuf,&recvlen); //判断报文长度是否正确 if(recvlen==304) lr_end_transaction("Trans_socket", LR_PASS); else lr_end_transaction ("Trans_socket", LR_FAIL); //判断返回信息的长度是否正确,recvlen处填入预期返回信息的长度 lrs_close_socket("socket0"); return 0; } |
data.ws 是报文部分, buf0 100 ,100是指报文的长度,\x表示是16进制