How to capture network packets
Disclaimer: This document may contain information about third-party products, which is for reference only. Ant Technology makes no implied or other form of commitment regarding the performance, reliability, or potential impact of third-party products.
Introduction
The network is the lifeline of mobile applications, and various network-level problems can cause many confusing behaviors and symptoms for users. By capturing network packet data, we can specifically analyze various symptoms caused by network-level problems, including connection interruptions, TLS handshake failures, DNS resolution failures, and other errors.
Unlike Charles/Fiddler, which can capture network packets through a "man-in-the-middle" proxy mode, TCP packet capture is generally non-intrusive. It directly captures "mirrored" TCP packets by listening to network interface data.
In general scenarios, there are many points where packets can be captured. They can be captured on the client side (A), on the intermediate device (B), or on the server side (C), as shown in the figure below.

TCP log capture tools
Charles and Fiddler can help capture and analyze HTTP layer problems. If the problem occurs at the TCP/IP layer, TCP packet capture and analysis tools are needed. Wireshark (supports Mac/Windows platforms), Network Monitor (Windows platform), and TCPDUMP are three commonly used network layer packet capture tools. Common network layer problems include SSL handshake failures and TCP connection interruptions and retransmissions.
Common packet capture tools
This section introduces the basic usage of Wireshark and TCPDUMP.
Wireshark
Download and install Wireshark.
Download the installation package from the official Wireshark website. After installation, the main interface lists all available network interfaces. The interface may differ slightly between macOS and Windows versions.

Start packet capture.
On the Wireshark main interface, the available network interfaces are listed.
Double-click the Wi-Fi: en0 interface to start capturing packets on that NIC.
Stop packet capture.
Click the red stop button in the menu bar (CMD+E) to stop the capture.
Click the save button (CMD+S) to save the captured packets.

TCPDUMP
TCPDUMP is a small and compact command-line network packet capture and analysis tool. While it is slightly less user-friendly than Wireshark, its advantage lies in its ability to run on more platforms and environments, facilitating packet capture directly on the client or server side.
Basic usage
TCPDUMP supports different parameters in different environments. It is recommended to use the man tcpdump command to confirm the currently supported parameter types and usage methods.
A common basic command combination is as follows:
// Capture the complete message and report the errors to a file
tcpdump -s 0 -w myCapture.pcapClick here to see more command.
Capture packets on a mobile device
As shown in the diagram at the top of the document, TCP packet capture for the client can be performed in two places:
Client exit
Network access point entry
This section describes how to capture packets at Point A (i.e., the client's exit point).
iOS platform
To capture network packets from an iOS client's outgoing network, the iOS mobile device needs to be connected to a MacBook via USB, and a virtual mapping of the device's network interface card (NIC) needs to be created on the Mac. Wireshark then uses this virtual NIC to capture network packets from the iOS mobile device.
Obtain the iPhone's UDID. Connect your iOS mobile device to your Mac via USB, and then use the following command in Terminal to obtain the iOS device's UDID (Serial Number):
$ system_profiler SPUSBDataType
Or, you can open Xcode > Window > Devices and Simulators to find the UID (Identifier).

Create a virtual NIC mapping.
$ rvictl -s < Your Device UUID > Starting device < Your Device UUID > [SUCCEEDED] with interface rvi0
Here,
rvi0is the name of the virtual NIC.Start packet capture (using Wireshark as an example). Open Wireshark; rvi0 will appear in the local interface list. See the image below:
Double-click rvi0 to enter the packet capture page. Once entered, packet capture will start automatically by default.
Stop packet capture. After reproducing the problem, if you need to stop packet capture, click the End button (CMD+E) in the menu bar to stop packet capture, and click the Save button (CMD+S) to save the packets.

Android platform
To capture packets from the Android client's outgoing network, you need to obtain root privileges on the device beforehand. You can then use ADB to call the tcpdump command on the device to capture packets.
Download TCPDump for Android.
Install TCPDUMP. Install TCPDUMP onto the device and grant execute permissions using the following command:
adb push tcpdump /data/local/tcpdump adb shell chmod 6755 /data/local/tcpdumpStart TCPDUMP to begin packet capture.
cd /data/local ./tcpdump -i any -p -s 0 -w /sdcard/myCapture.pcapClick here to view detailed TCPDUMP parameter configuration.
Stop packet capture and retrieve data. When you need to stop packet capture after reproducing the problem, follow the prompts to stop packet capture (Ctrl+C). Copy the packet data using the following command:
adb pull /sdcard/myCapture.pcap
Capture packets on an intermediate device
As shown in the diagram at the top of the document, TCP packet capture for the client can be performed in two places:
Client exit
Network access point entry
This section describes the packet capture method at Point B (i.e., the access device entry point).
Packet capture network topology configuration. A common network connection configuration for this packet capture mode is as follows: Use a Mac or PC as the packet capture device. This machine needs to be pre-configured with dual network cards, one of which needs to be capable of broadcasting a Wi-Fi hotspot. The client connects to the configured Wi-Fi hotspot via Wi-Fi, while the other network card of the Mac or PC connects to the normal network.

Start packet capture (using Wireshark as an example). Open Wireshark, locate the network card of the wireless hotspot connected to your phone (network card 1 in the image above), and double-click the network card icon to start packet capture.
Stop packet capture. After reproducing the problem, if you need to stop packet capture, click the End button (CMD+E) in the menu bar to stop packet capture, and then click the Save button (CMD+S) to save the packets.
There are many types of "intermediate devices," and this section only introduces one of them. In practice, different tools can be used to perform the same type of packet capture on different devices. It is important to note that you should try to capture packets on the first access device to avoid interference from intermediate processes.
Capture packets on the server
As shown in the diagram at the top of the document, some issues require packet capture to be started on the server side. This section uses TCPDUMP as an example.
Install TCPDUMP.
On CentOS:
yum install tcpdumpOn Debian and Ubuntu:
apt-get install tcpdumpFor more information, see the official TCPDUMP website.
Start packet capture. TCPDUMP supports many parameters — configure them for your specific scenario. For example:
tcpdump -s 0 -w myCapture.pcapFor the full parameter reference, see the official TCPDUMP documentation.
Stop packet capture. After reproducing the problem, press Ctrl+C to stop the capture and save the captured packets to a suitable location.



Double-click rvi0 to enter the packet capture page. Once entered, packet capture will start automatically by default.

