Structure of TCP/UDP header
Structure of the TCP header
To understand TCP communication, it is necessary to understand what is contained in the TCP header. TCP establishes a connection prior to communication, and performs acknowledgment and flow control, so the TCP header structure is much more complicated than the UDP packet structure. A typical TCP header size is 20 bytes large.

field | explanation |
Source port number (16 bits) | A number that identifies the sending application. Unlike UDP, TCP cannot have a source port number of 0. Available from 1 to 65535. |
Destination port number (16 bits) | A number that identifies the destination application. Available from 1 to 65535. *0 is reserved |
Sequence number (32 bits) | Number for ordering data to be sent. Increment the sequence number by 1 for each byte of data sent. 2 If it exceeds 32 , repeat the same number again. |
Acknowledgment number (32 bits) | A field that indicates how much of the received data has been received by byte position. Returns the sequence number + 1 of the received data position. The ACK number field is valid only when the ACK flag is ON. |
Header length (4 bits) | A field that indicates where the TCP data begins. Since the data part immediately follows the TCP header, it can be considered as the size of the TCP header. |
URG (1 bit) | URG ... urgent: A flag indicating that urgent urgent data is included. Default value is 0, 1 turns ON. Not used much. |
ACK (1 bit) | ACK --- acknowledge A flag indicating that a valid ACK number is included in the TCP header. All other TCP packets have the ACK flag turned ON, except for the very first one during the TCP 3-way handshake. |
PSH (1 bit) | PSH ・・・ A flag for requesting that the received push data be delivered to the application immediately. Telnet turns this flag ON because buffering can degrade responsiveness. |
RST (1 bit) | RST ・・・ reset A flag that is set when you want to interrupt or reject a TCP connection. By sending a TCP packet with the RST flag ON, the current TCP connection can be forcibly terminated. |
SYN (1 bit) | SYN --- synchronize At the start of open processing at the time of TCP 3-way handshake, each side turns ON the SYN flag and synchronizes the ACK number. It is not set in subsequent packets. |
FIN (1 bit) | FIN - flag set to terminate the finis TCP connection. A TCP connection is terminated when a FIN is sent from both sides. |
Window size (16 bits) | A field used to convey the receiver's window size. The unit is bytes, and the maximum is 65535 bytes. 0 indicates that no data can be received. |
Checksum (16 bits) | A field containing test data for checking the integrity of TCP packets. |
Urgent pointer (16 bits) | Valid only if the URG flag is 1. Represents the location of urgent data. |
option | A variable-length field used to set characteristics of a TCP connection. It is used for exchanging MSS. Padding (0) at the end if necessary to make it a multiple of 32 bits. |
data | Data part of TCP. In some cases, packets are sent with only TCP headers and no data to prevent the TCP connection from timing out and being closed. |
Structure of the UDP header
UDP has the advantage of being more real-time than TCP because there are no acknowledgments, retransmissions, or congestion control that impair real-time. UDP headers are very simple compared to TCP headers. It only has 8 bytes.

field | explanation |
Source port number (16 bits) | A number that identifies the sending application. For UDP packets that do not request a reply, set the source port number to 0. Available from 0 to 65535. |
Destination port number (16 bits) | A number that identifies the destination application. Available from 0 to 65535. |
Header length (4 bits) | A field that represents the length of the UDP packet. Stores the number of bytes plus the length of the data part sent by UDP. |
Checksum (16 bits) | A field containing inspection data for checking the integrity of UDP packets. The checksum calculation uses 3 parts: UDP pseudo-header (12 bytes), UDP header (8 bytes), and UDP payload. *UDP pseudo-header is a virtual header that is used only when calculating the checksum. |
data | Data part of UDP. |