---
title: "[W55RP20] How to set 2 types (Passive & Active) of Keepalive packet on TCP/IP"
url: "https://maker.wiznet.io/lawrence/projects/w55rp20-how-to-set-2-types-passive-active-of-keepalive-packet-on-tcp-ip/"
markdown_url: "https://maker.wiznet.io/lawrence/projects/w55rp20-how-to-set-2-types-passive-active-of-keepalive-packet-on-tcp-ip/md"
type: "WCC: WIZnet Created Content"
author: "lawrence"
author_url: "https://maker.wiznet.io/lawrence/"
original_author: "Lawrence"
license: "MIT license (MIT)"
published: "2024-10-31"
language: "en"
tags: ["W55RP20"]
hardware: ["WIZnet W55RP20-EVB-Pico"]
likes: 0
views: 3898
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# [W55RP20] How to set 2 types (Passive & Active) of Keepalive packet on TCP/IP

> W55RP20 can send keepalive packet on TCP mode. There are 2 ways to set Keepalive function

Original author: Lawrence

## Components

- **WIZnet W55RP20-EVB-Pico** x 1 ([docs](https://wiznet.io/products/powered-by-raspberry-pi/w55rp20-evb-pico))

## Documents and links

- [WIZnet-PICO-C](https://github.com/WIZnet-ioNIC/WIZnet-PICO-C) (code)

## Article

### Project description

In TCP communication, **Keepalive** is a feature that periodically sends small packets to check if a connection is still active. This helps detect any abnormal disconnections and regularly confirms that the connection remains alive.

#### **Functions of TCP KeepalivePeriodic Connection Check:**

- Keepalive sets both connected systems to send and receive Keepalive packets at specific intervals. This allows verification of the connection's validity even when there’s no actual data transmission.

**Detecting Abnormal Connection Termination**:

- When a network failure or an unexpected shutdown of the remote system causes a disconnection, Keepalive can detect it and terminate the connection.

- If there is no Keepalive response within a specified period, the system assumes the connection is broken and can take action, such as closing the connection or retrying it.

#### Importance of TCP Keepalive

**Resource Management**:

- Maintaining a broken TCP connection wastes system resources. Keepalive automatically terminates inactive connections, helping manage resources efficiently.

**Connection Status Check**:

- It’s useful for monitoring the status of long-standing idle connections. For example, when IoT devices need to stay connected to a server, Keepalive can periodically check the connection status.

**Network Failure Detection**:

- Keepalive allows quick detection of sudden network disconnections during a maintained TCP connection, reducing service interruptions and data loss.

![](https://maker.wiznet.io/upload/ckeditor5/264347937%5F1730370817%2Epng)

### How to set TCP Keepalive in W55RP20

There are two ways to set up TCP Keepalive on the W55RP20. One is the passive keepalive method, which terminates TCP communication with a timeout if there is no response from the other party for a set period of time, and the active method, which continuously sends keepalive packets to the other party at a set time interval until the other party responds.

- Passive method : Terminate TCP connections, after no response in a certain amount of time

- Active method : No disconnection, continuous keepalive packets sent at regular time intervals.

** All TCP retransmissions must transfer at least 1 byte of data. This is because the sequence number between data transmissions is set to the last sequence number -1 for the other party's ACK transmission.*

**How to set Passive TCP keepalive :**

The W55RP20, which includes the W5500, can set passive keepalive behavior by setting the RTR (Retry Time-value register) and RCR (Retry Count Register).
This behavior transmits packets as many times as set by RCR based on the time set by RTR when there is no normal response from the peer, and the transmission interval of each packet increases according to the exponential backoff rule.

![](https://maker.wiznet.io/upload/ckeditor5/264347937%5F1730388862%2Epng)

If there is no response from the peer after sending the RCR number of packets, a Timeout Interrupt is generated and the TCP connection is forcibly terminated.

![](https://maker.wiznet.io/upload/ckeditor5/264347937%5F1730390015%2Epng)

**How to set Active TCP keepalive :**

Setting up active tcp keepalive packet sending is simpler than the passive method. If Sn_KPALVTR is set to at least 1, keepalive packets are sent at intervals equal to 5 seconds times that value.

At least 1 byte of data must be transmitted for the keepalive packet to start sending.

![](https://maker.wiznet.io/upload/ckeditor5/264347937%5F1730388739%2Epng)

If you set the Sn_KPALVTR value to 2, you can see that the keepalive packet is sent every 10 seconds as shown below. And even if it receives an ACK response from the peer, it is forced to resend the keepalive packet after that time.

(https://github.com/WIZnet-ioNIC/WIZnet-PICO-C/blob/main/examples/loopback/w5x00_loopback.c)

![](https://maker.wiznet.io/upload/ckeditor5/264347937%5F1730389168%2Epng)

![](https://maker.wiznet.io/upload/ckeditor5/264347937%5F1730388779%2Epng)

If you look at the keepalive packet sending interval, you will see that it is constant, but the time interval is larger than the set 10 seconds (e.g., 10.2 seconds). This is because the time interval includes the Retry time-value set in RTR in addition to Sn_KPALVTR.
If you decrease the RTR value, it will be approximated to the set value.

![](https://maker.wiznet.io/upload/ckeditor5/264347937%5F1730389173%2Epng)

![](https://maker.wiznet.io/upload/ckeditor5/264347937%5F1730388759%2Epng)

Below you can see the keepalive packets sent every 20 seconds when Sn_KPALVTR is set to 4.

```c
uint8_t kpalvtr = 4;
```

![](https://maker.wiznet.io/upload/ckeditor5/264347937%5F1730388786%2Epng)

### Conclusion

Keepalive packets are not commonly utilized, but due to the nature of TCP connections, managing idle connections is a critical component of resource efficiency. Keepalives can be effectively utilized in a serverless operating policy to detect abnormal connections on TCP connections and either remove them or keep normal connections alive. The W55RP20 has a keepalive behavior to respond to a lack of response from the other party, but it also provides an active method of keepalive to prevent disconnection from the server due to persistent TCP idleness.

---

Source: https://maker.wiznet.io/lawrence/projects/w55rp20-how-to-set-2-types-passive-active-of-keepalive-packet-on-tcp-ip/
