top of page
  • Writer's pictureLingheng Tao

Unity Engine #2 System / Networking


This note is mainly about computer systems and network knowledge.


Principles of network communication


Transmission Control Protocol / TCP


Transmission Control Protocol is referred to as TCP. This is a network communication protocol.


[Basic Features]

  1. Connection-oriented: Before data transmission, a connection must be established first;

  2. Reliable transmission: Ensure the correct transmission of data through sequence numbers, confirmation responses, retransmission mechanisms, etc.

  3. Ordered transmission: Data arrives at the receiver in the order it was sent.

  4. Congestion Control: Dynamically manage data transmission speed to prevent network congestion.

  5. Flow control: Adjust the sending rate through the window mechanism to prevent the receiver from processing in time.

【Working principle】

3-way Handshake


First handshake: The client sends a SYN packet (seq = x) to the server and enters the SYN_SENT state;

Second handshake: The server receives the SYN packet, responds with a SYN+ACK packet (seq = y, ack = x+1), and enters the SYN_RCVD state;

Third handshake: The client receives the SYN+ACK packet, sends an ACK packet (ack = y+1), and enters the ESTABLISHED state.


4-way Handshake

The first wave: the initiator sends a FIN (seq = u) packet and requests to disconnect;

The second wave: the receiver confirms the FIN packet and sends an ACK (seq = v, ack = u+1) packet;

The third wave: the receiver sends a FIN (seq = w, ack = u+1) packet;

The fourth wave: the initiator confirms the FIN packet and sends an ACK (seq = u+1, ack = w+1) packet;


TCP header structure

TCP header (TCP Header) is 20 bytes in total. Among them

1. Source Port (Source Port): 16 Bits = 2 Bytes, the corresponding type is short.

2. Destination Port (Destination Port): 16 Bits = 2 Bytes, the corresponding type is short.

3. Sequence Number (Sequence Number): 32 Bits = 4 Bytes, the corresponding type is uint.

4. Confirmation Number (Acknowledge Number): 32 Bits = 4 Bytes, the corresponding type is uint. The confirmation number field is valid only when ACK=1. When ACK=0, the confirmation number is invalid.

5. Data Offset (Header Length) + Reserved Bits (Reserved) + Control Bits (Code Bits): 4+6 +6 = 16 Bits = 2 Bytes. These three data are added together and stored in a short, so bit operations must be used when calculating them.

Note: Control bit: UAPRSF corresponds to URG emergency packet/ACK confirmation packet/RST reset packet/SYN synchronization packet/FIN termination packet respectively.

6. Window size (Window): 16 Bits = 2 Bytes, the corresponding type is short. The window field is used to control the amount of data sent by the other party, in bytes. This means that the window size can only be a size between 0 - 65535 bytes. One end of the TCP connection determines its own receiving window size based on the set cache space size, and then notifies the other party to determine the upper limit of the other party's sending window.

7. Checksum (Checksum): 16 Bits = 2 Bytes, the corresponding type is short.

8. Urgent pointer (Urgent): 16 Bits = 2 Bytes. When URG = 1 (in the control bit above), indicates that the urgent pointer field is valid. It tells the system that there is urgent data in this segment and should be transmitted as soon as possible (equivalent to high-priority data).

Total 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 = 20 Bytes.


User Datagram Protocol / UDP


User Datagram Protocol (User Datagram Protocol), referred to as UDP, is another network communication protocol.


[Basic Features]

  1. No connection: There is no need to establish a connection before data transmission;

  2. Unreliable transmission: There is no guarantee of reliable arrival of data, and there is no retransmission mechanism.

  3. Out-of-order transmission: The order in which data arrives at the receiver is not fixed.

  4. No congestion control:Unable to manage data transfer speeds, which may lead to network congestion.

  5. Flow control:The packet header information is less and the processing speed is fast.

【Working principle】

  1. Data sending: sending data directly, there is no guarantee that the other party can receive it;

  2. Data reception: There is no need to confirm with the other party when receiving data, and the data processing starts directly.

[UDP header structure]

Only 8 bytes.


Similar to TCP, the source port and destination port each occupy 2 bytes. The checksum and length each occupy 2 bytes. Below is the UDP data, so the header is very small.


[Comparison with TCP]

​TCP

UDP

​Applicable scenarios

Suitable for applications requiring high reliability, such as web browsing, file transfer, email, etc.

​Suitable for applications with higher real-time performance, such as video streaming, online games, VoIP, etc.

Performance differences

Due to mechanisms such as connection establishment and confirmation responses, the delay is high, but the reliability is high.

Low latency and high throughput, but data integrity and order are not guaranteed.

Disconnection and reconnection mechanism


For most applications that require a disconnection and reconnection mechanism, TCP is a more suitable choice because the built-in reliable transmission mechanism can simplify the design. Two disconnection and reconnection mechanisms can be designed as follows.

TCP disconnection and reconnection

1. Check connection status

  • Both the client and the server need a mechanism to detect the current connection status.

  • You can check whether the connection is still active by sending heartbeat messages periodically.

2. Identify disconnection

  • When the heartbeat detection fails or an error occurs in the TCP connection, the client should identify it as a "disconnected" state.

  • The server also needs to detect client disconnection and mark the client as inactive or disconnected.

3. Save status

  • The server needs to save the current state of the client when a disconnection occurs.

  • Status information can include player position, scores, items, etc.

4. Try to reconnect

  • After detecting a disconnection, the client should try to reconnect to the server.

  • You can set a time interval for reconnection attempts and the maximum number of attempts.

5. Authentication and state recovery

  • When reconnecting, the client needs to send authentication information to the server.

  • After the server verifies the client's identity, it restores the client's previous state to the client.

6. Resume session

  • Once the reconnection is successful and the state is restored, the client can continue from the state before the disconnection.

7. Handling failed reconnect attempts

  • If the reconnection attempt fails, the client can prompt the user and provide the option to try again or exit.

UDP disconnection and reconnection





 

Reference materials:

  1. Detailed explanation of TCP/IP packet structure: https://www .cnblogs.com/larry-luo/p/10983633.html

3 views0 comments

Recent Posts

See All
bottom of page