First, socket background knowledge
I'm not going to talk about it, I'll have a big search on the Internet.
Second, the implementation of this example of the function
The server receives the string sent by the client and returns "5678SUCC" with a total of 8 characters
Third, server implementation (Java code)
①mysocketserver.java
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
{
Create a server-side socket, specify the bound port, and listen for this port
ServerSocket serversocket=new ServerSocket (8820);
@SuppressWarnings ("resource")
ServerSocket serversocket=new ServerSocket (8820, Inetaddress.getbyname ("9.111.42.204"));
Socket Socket=null;
int count=0;
SYSTEM.OUT.PRINTLN ("* * * server is about to start, waiting for client links * * *");
Loop monitoring waiting for a client link
while (true)
{
Call the Accept () method to start listening
Socket=serversocket.accept ();
Create a new thread
Serverthread serverthread=new serverthread (socket);
Start thread
Serverthread.start ();
Number of count++;//statistics clients
SYSTEM.OUT.PRINTLN ("Client:" +count+ "second Visit");
InetAddress address=socket.getinetaddress ();
System.out.println ("IP of Current client:" +address.gethostaddress ());
}
}
catch (IOException E)
{
E.printstacktrace ();
}
}
}
②serverthread.java
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 associated with this thread
Socket socket = NULL;
Public serverthread (socket socket) {
This.socket = socket;
}
A thread performs an action that responds to a client's request
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 {
Gets the input stream and reads the client information
is = Socket.getinputstream ();
ISR = new InputStreamReader (IS);
br = new BufferedReader (ISR);
Out1=new DataOutputStream (Socket.getoutputstream ());
String Info=null;
while ((Info=br.readline ())!=null) {//loop read the client's information
SYSTEM.OUT.PRINTLN ("Message from client:" +info);
}
Socket.shutdowninput ();
Out1.writeutf ("1234"); This method returns twice times the length of the actual character. will increase the processing difficulty of the client
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);//The best way is to use a byte array, otherwise the client program calling the socket server may not parse properly
Out1.flush ();
Socket.shutdownoutput ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
Close Resource
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 ();
}
}
}
}
Iv. Client Implementation (LoadRunner script)
①action.c
#include "Lrs.h"
Action ()
{
Char *data;
int size=0;
int rc=0;
Returns whether the message is successful, the value
int msgok=-1;
Char *position= "";
Returns whether the message was successfully identified
Char *passmsg= "SUCC"; Need to confirm with the server whether the string must be returned
int Receive_code;
memset (&data,0,8);
Creating sockets
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);
}
Start a transaction
Lr_start_transaction ("Socket_trans");
Send data
Lrs_send ("Socket0", "buf0", Lrslastarg);
End Send
Lrs_disable_socket ("Socket0", disable_send);
Receive data sent back by the server
Receive_code=lrs_receive ("Socket0", "Buf1", Lrslastarg);
Gets the data of the last returned buffer and its length
Lrs_get_last_received_buffer ("Socket0", &data,&size);
Gets the location of the passmsg in the returned data
position= (char *) strstr (data,passmsg);
msgok= (int) (position-data+1); Array subscript starting from 0, so add 1 to conform to the usage habit
Save parameters
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>"));
Determines whether a transaction is successful, based on the value of Msgok
if (msgok>0) {
Lr_end_transaction ("Socket_trans", Lr_pass);
}
Else
{
Lr_end_transaction ("Socket_trans", Lr_fail);
}
Close the open socket
Lrs_close_socket ("Socket0");
return 0;
}
②data.ws
; Wsrdata 2 1
Send buf0 10
"<NewParam>"
Recv BUF1 8
-1
LoadRunner writing an instance of the socket script (with server-side implementation)