China Naming Network - Auspicious day query - How to write a message forwarding script in C language?

How to write a message forwarding script in C language?

Simple writing method of socket protocol script.

Introduction of 1.socket protocol

Socket protocol is called general protocol, and many systems use Socket protocol at the bottom, which is widely used.

1. 1 description of socket communication mode

There are two ways to communicate with socket. One is to establish a long connection and then keep sending and receiving. The other is to establish a short connection, that is, establish a connection to send a message and receive a message to close the connection.

Introduction of buf types sent by 1.2 Socket protocol

Send buffer types are divided into string and xml types.

1.3 Socket protocol script premise:

Communicate with the project team to confirm whether it is socket protocol. The project team will provide the server IP and port number, as well as the sending and receiving message pairs of socket protocol transactions, as well as transaction interface documents, so as to understand the data length, parameterized fields, structure and its representative content of the messages, and then develop socket protocol scripts.

1.4, description and examples of Socket protocol script functions:

1)Name LRS _ create _ socket();

Create a socket connection, add IP and port number, and return 0 if it is created successfully, otherwise return a value other than 0. (For long connections, set up a socket connection and put it in the vuser_init function, and put the short connection in the Action. Example: LRS _ create _ socket ("socket 0", "TCP", "Remote host =180.170.150.230: 7700".

2) Name LRS _ send ();

Send socket request message, get the message in buffer buf0 and send it.

For example: LRS _ send ("socket 0 ","buf 1 ",LRSlatarg);

3) Name LRS _ receive ();

Receive the response message from socket and put it in buf 1.

For example: LRS _ receive ("Socket 0 ","BUF 2 ",LRSlaStarg);

4)Name LRS _ get _ last _ received _ buffer();

Gets the last received buffer and size, where the value of the last received buffer is assigned to the RecvBuf variable and the size is assigned to RecvLen.

For example: LRS _ Get _ Last _ Receive _ Buffer ("socket0",&; recvBuf & amp; recv len);

5) Name LRS _ free _ buffer ();

To prevent memory leakage, please free up memory space.

For example: LRS _ free _ buffer (recvbuf);

6) Name LRS _ close _ socket ();

Close the socket connection. (For long connections, closing the socket connection should be placed in the vuser_end function. )

For example: LRS _ close _ socket ("Socket0");

Other commonly used socket functions:

lrs_set_send_buffer("socket0 ",sSendPkg,iLenOfPkg); //Specify the socket information to be sent.

lrs_get_buffer_by_name("buf0 ",sSendPkg,iLenOfPkg); //Get the received buffer and size.

lrs_length_send("socket0 "," buf0 ", 1," Size=4 "," Encoding= 1 ",LrsLastArg);

Correlation function:

lrs_save_param_ex("socket0 "," received "," 15 1,7," ascii "," response "); //Take the string at the specified position and save it in a variable to judge whether the transaction is successful.

LRS _ save _ search _ string(); //Searches for a string at the specified location and reports the string displayed as an error to the parameter.

Timeout function

LRS _ set _ connect _ time out(); //Set connection timeout

LRS _ set _ recv _ time out(); //Set the server response timeout.

LRS _ set _ recv _ time out 2(); //Set reception