1、socket背景知識java
這個咱就不廢話了,網上一搜一大堆數組
2、本實例實現的功能服務器
服務端接收客戶端發送的字符串,並返回"5678succ"共8個字符socket
3、服務端實現(java代碼)this
①MySocketServer.java.net
package serverSocketMultiThreadVer;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MySocketServer {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
//建立一個服務器端socket,指定綁定的端口,並監聽此端口
//ServerSocket serverSocket=new ServerSocket(8820);
@SuppressWarnings("resource")
ServerSocket serverSocket=new ServerSocket(8820, 100, InetAddress.getByName("9.111.42.204"));
Socket socket=null;
int count=0;
System.out.println("***服務器即將啓動,等待客戶端的連接***");
//循環監聽等待客戶端的連接
while(true)
{
//調用accept()方法開始監聽
socket=serverSocket.accept();
//建立一個新的線程
ServerThread serverThread=new ServerThread(socket);
//啓動線程
serverThread.start();
count++;//統計客戶端的數量
System.out.println("客戶端第:"+count+"次訪問");
InetAddress address=socket.getInetAddress();
System.out.println("當前客戶端的IP:"+address.getHostAddress());
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
線程
②ServerThread.javacode
package serverSocketMultiThreadVer;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThread extends Thread {
// 和本線程相關的Socket
Socket socket = null;
public ServerThread(Socket socket) {
this.socket = socket;
}
//線程執行的操做,響應客戶端的請求
public void run(){
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
OutputStream os=null;
PrintWriter pw=null;
DataOutputStream out1=null;
byte[] mybyte=new byte[8];
try {
//獲取輸入流,並讀取客戶端信息
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
out1=new DataOutputStream(socket.getOutputStream());
String info=null;
while((info=br.readLine())!=null){//循環讀取客戶端的信息
System.out.println("來自客戶端的消息:"+info);
}
socket.shutdownInput();
//out1.writeUTF("1234"); 這種方法返回長度是實際字符的2倍。會增長客戶端的處理難度
mybyte[0]=53;
mybyte[1]=54;
mybyte[2]=55;
mybyte[3]=56;
mybyte[4]='s';
mybyte[5]='u';
mybyte[6]='c';
mybyte[7]='c';
//out1.writeChars("5678");
out1.write(mybyte);//最好的方式是使用字節數組,不然調用該socket服務端的客戶端程序有可能不能正常解析
out1.flush();
socket.shutdownOutput();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//關閉資源
try {
if(out1!=null)
out1.close();
if(pw!=null)
pw.close();
if(os!=null)
os.close();
if(br!=null)
br.close();
if(isr!=null)
isr.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
server
4、客戶端實現(loadrunner腳本)事務
①Action.c
#include "lrs.h"
Action()
{
char *Data;
int Size=0;
int rc=0;
//返回報文是否成功,判斷值
int msgOk=-1;
char *position="";
//返回報文是否成功標識
char *passMsg="succ"; //需和服務端進行確認是否必定返回該字符串
int receive_code;
memset(&Data,0,8);
//創建socket
rc=lrs_create_socket("socket0","TCP","RemoteHost=9.111.42.204:8820",LrsLastArg);
if (0==rc) {
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("socket_trans");
//發送數據
lrs_send("socket0","buf0",LrsLastArg);
//結束髮送
lrs_disable_socket("socket0", DISABLE_SEND);
//接收服務端發回的數據
receive_code=lrs_receive("socket0","buf1",LrsLastArg);
//獲取最後返回的緩衝區的數據及其長度
lrs_get_last_received_buffer("socket0",&Data,&Size);
//獲取passMsg在返回數據的位置
position=(char *)strstr(Data,passMsg);
msgOk=(int)(position-Data+1); //數組下標從0開始,所以加1以符合使用習慣
//保存參數
lrs_save_param_ex("socket0","user",Data,0,8,"ascii","new_parameter");
lrs_free_buffer(Data);
lr_output_message("The result's size is: %d.",Size);
lr_output_message("The receive code is: %d.",receive_code);
lr_output_message("The data socket server give is: %s",lr_eval_string("<new_parameter>"));
//根據msgOk的值,判斷事務是否成功
if (msgOk>0) {
lr_end_transaction("socket_trans",LR_PASS);
}
else
{
lr_end_transaction("socket_trans",LR_FAIL);
}
//關閉打開的socket
lrs_close_socket("socket0");
return 0;
}
②data.ws
;WSRData 2 1send buf0 10 "<NewParam>"recv buf1 8-1