Comprehensive Guide to JMeter TCP Testing: Understanding the Protocol, Sampler Parameters, and Wireshark Packet Capture

This article mainly introduces JMeter TCP testing and how to use JMeter for testing the TCP protocol. 1. Concept of TCP 2. Three-way handshake of the TCP protocol 3. Introduction to TCP sampler parameters 4. Packet capture with Wireshark and TCP script development

I. Concept of TCP

1. TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol that fulfills the functions specified by the fourth layer transport layer in the simplified OSI model of computer networking. User Datagram Protocol (UDP) is another important transport protocol at the same layer. During data transmission, applications send datastreams to the TCP layer, which segments the received datastream into packets (the size of which is adjusted based on the current network environment), and then passes them down through subsequent layers until they reach the TCP layer of the target node. To prevent packet loss, the TCP protocol assigns sequence numbers to packets and requires acknowledgment (ACK) from the recipient. If packets are not acknowledged, they are retransmitted. This process is the commonly known TCP connection’s three-way handshake. TCP also uses parity checking to verify whether errors occurred during data transmission.

II. The Three-way Handshake of the TCP Protocol

1. First handshake: The client sends a SYN packet (seq=x) to the server and enters the SYN_SEND state, waiting for the server’s acknowledgment; 2. Second handshake: The server receives the SYN packet and must acknowledge the client’s SYN (ack=x+1). It also sends a SYN packet of its own (seq=y), forming the SYN+ACK packet, and then enters the SYN_RECV state; 3. Third handshake: The client receives the server’s SYN+ACK packet and sends back an acknowledgment packet ACK (ack=y+1) to the server. With this, the client and server both enter the ESTABLISHED state, completing the three-way handshake. 4. No data is transmitted in the packets exchanged during the handshake; actual data transmission begins only after the three-way handshake is complete. In an ideal state, once a TCP connection is established, it will be maintained until one of the communicating parties closes it.

5. Actual packet capture validation: In 16734 -> 808, 16734 is the local (client) port, while 808 is the server’s port. The three exchanges between port 808 and port 16734 constitute the “three-way handshake” process.

Notice that in the “first handshake”, the TCP packet sent by the client is marked with [SYN] as the flag and the client’s sequence number is Seq=0. In the “second handshake”, the TCP packet returned by the server uses [SYN, ACK] as the flag, with the server’s sequence number Seq=0 and acknowledgment number Ack=1 (value of the client’s sequence number from the “first handshake” +1). In the “third handshake”, the client sends a TCP packet to the server marked with [ACK] as the flag.

III. Introduction to TCP Sampler Parameters

1. Open JMeter, click Test Plan > Add > Config Element > TCP Sampler Config

2. The TCP Sampler provides three types of message encoding implementations, which are

org.apache.jmeter.protocol.tcp.sampler.TCPClientImp org.apache.jmeter.protocol.tcp.sampler.BinaryTCPClientImpl org.apache.jmeter.protocol.tcp.sampler.LengthPrefixedBinaryTCPClientImpl 1) TCPClientImpl sends plain text as edited in a text editor. 2) BinaryTCPClientImpl converts hexadecimal characters (hex) edited in a text editor to binary byte content for sending. 3) LengthPrefixedBinaryTCPClientImpl sends content prefixed with the length in bytes based on the BinaryTCPClientImpl.

3. 1) Re-use connection: If selected, the connection remains open; otherwise, it closes after reading the data. 2) Close connection: If selected, the TCP Sampler closes after execution. 3) SO_LINGER: This configuration item controls whether to wait for buffered data to be sent before closing the connection. If SO_LINGER is specified with a value, it waits for the specified number of seconds to finish sending the buffered data after a close request, then closes the connection. Setting this option to 0 causes all connections to close immediately upon receiving a close request, avoiding many sockets in the TIME_WAIT state. 4) End of line (EOL) byte value: The last two bytes of the response data, converted to decimal. Value range [-128,127] 5) Connect Timeout: Connection timeout time (milliseconds) with the server socket application. 6) Response Timeout: Response timeout time (milliseconds); this value is related to the End of line (EOL) byte value. If the value in End of line (EOL) byte value is set incorrectly, JMeter may wait indefinitely, but if Response Timeout is specified, the connection closes when this value is reached. 7) Set No Delay: Depending on whether this option is needed, it should be combined with the actual business situation.

IV. Packet Capture with Wireshark and TCP Script Development

1. Open Wireshark, then double-click to select Microsoft: WLAN WiFi network

2. Then open the system that needs packet capture, perform login and query operations; the requests captured are as follows

1) Display filter: Used for filtering, it can filter needed requests based on IP and TCP streams; 2) Packet list: Displays captured packets with source and destination addresses and port numbers. Different colors in the list represent different protocols; 3) Packet details: Displays fields of the selected line of packet; 4) Hex data: Displays the hexadecimal data corresponding to the packet details of the selected line;

3. Filters can be added to the capture page, click the + button, enter the label and filter, click OK, and the filter will display on the right side of the filter for easy use next time, as shown below, ip.dst==10.16.24.165, representing requests targeting server IP 10.16.24.165.