---
# System prepended metadata

title: Networking Programming - Ping

---

# Networking Programming - Ping

## What is Ping?

<div style="text-align:center; margin-bottom: 30px;">
  <img src="https://hackmd.io/_uploads/BJS7tgltp.png" alt="logo iperf" width="500" />
</div>

A ping, or Packet Internet or Inter-Network Groper, functions as a fundamental Internet tool in computer network administration. It allows users to test the existence and responsiveness of a specific destination IP address. The term "ping" was coined to match the sound of a returned sonar pulse used by submariners. Beyond its diagnostic role in ensuring the operational status of a host computer, ping is widely supported by operating systems and embedded network administration software. For instance, Windows or Ubuntu users can use the command prompt (start/run/cmd) to input "ping xxxxx.yyy" (where "xxxxx" is the second-level domain name like "abdfikih," and "yyy" is the top-level domain name like "site") to discover the corresponding dot address, such as  192.168.1.10, for a given domain name.

## How it's Work?

Ping operates by using the Internet Control Message Protocol (ICMP) to assess the connectivity and round-trip time (RTT) between two devices on a network. Here's a step-by-step explanation of how ping works:

1. **User Initiates Ping Command** : The user initiates a ping command from computer's command line or terminal. Specify the target destination by providing an IP address or domain name.
2. **ICMP Echo Request** : The computer sends an ICMP Echo Request packet to the specified destination IP address. This packet contains a unique identifier and a sequence number.
3. **Router and Network Transmission** : The ICMP Echo Request packet is transmitted through the user's local network to the destination network. Routers along the way forward the packet toward the destination.
4. **Destination Host Receives Packet** : The destination host receives the ICMP Echo Request packet.
5. **ICMP Echo Reply** : The destination host processes the incoming packet and responds by sending back an ICMP Echo Reply. This reply contains the same unique identifier and sequence number as the original request.
6. **Packet Return Journey** : The ICMP Echo Reply travels back through the network, reaching the user's computer.
7. **Round-Trip Time (RTT) Calculation** : The user's computer calculates the round-trip time (RTT), which is the time taken for the ICMP Echo Request to reach the destination and for the corresponding ICMP Echo Reply to return. The RTT is often measured in milliseconds (ms).
8. **Displaying Results** : The ping command displays information about the success of each packet transmission, including details such as the number of bytes sent and received, the time taken for the round trip, and any potential packet loss.

## Test with Ping

<div style="text-align:center; margin-bottom: 30px;">
  <img src="https://hackmd.io/_uploads/BkmwZQgKa.png" alt="logo iperf" width="200" />
</div>

Ping, as a versatile networking tool, extends its utility beyond basic connectivity testing and troubleshooting. it is also instrumental in assessing the speed and responsiveness of data transmission between different points in a network. This additional facet of ping, commonly referred to as a "ping test" or "ping on a speed test," involves evaluating the time it takes for a data signal to travel from a source, such as a computer, to a destination, such as a website.

In a ping test scenario, a user initiates the ping command, directing it towards a specific destination, typically a website or server. The ICMP Echo Request packet is sent to the target, and upon its reception, the destination responds with an ICMP Echo Reply. The round-trip time (RTT) calculation, measuring the time taken for the signal to travel to the destination and back, provides crucial insights into the responsiveness of the network.

This speed test aspect of ping becomes particularly relevant when evaluating the efficiency of data transmission for web applications and services. Users can analyze the RTT values obtained from ping tests to gauge the latency or delay experienced during data exchange. Lower RTT values generally indicate faster and more responsive connections, while higher values may suggest potential latency issues in the network.

## Make a Ping

To run the ping utility, a user should enter the ping command followed by the target device hostname or IP address into a command-line interface. Here the basic syntax of this command and some examples:

Syntax:
* ping <space> <IP address or hostname>
    
Example:
* ping 127.0.0.1
* ping abdfikih.site

    
The ping utility was initially developed for the Unix OS. Later, most operating systems started supporting ping, but unfortunately, they introduced their own customizable parameters lacking consistency. To set the number of echo requests in a ping network test, for example, Windows uses the -n (number) option, whereas Unix (Ubuntu) leverages the -c (count) parameter.

Example:

* Windows: ping -n 5 abdfikih.site
<div style="text-align:center; margin-bottom: 30px;">
  <img src="https://hackmd.io/_uploads/r1SIvQlta.png" alt="logo iperf" width="400" />
</div>

* Unix: ping -c 4 abdfikih.site
<div style="text-align:center; margin-bottom: 30px;">
  <img src="https://hackmd.io/_uploads/Sy5psQetT.png" alt="logo iperf" width="400" />
</div>

The parameters added after the ping command are known as the ping switches or modifiers. Typically, the command “ping ?” allows IT staff to get details for all the ping options they can use on any operating system. Outlined below are some of the popular switches in Windows:

* -t: Use this option to continuously send ping requests to the target system.
* -a: This option helps unveil the hostname of the target IP address.
* -n: This option sets the number of echo requests.
* -l: This ping parameter specifies the size of an echo request packet, typically in bytes.
* -f: This IPv4-centric option prevents echo request fragmentation by intermediary routers.
* -i: This ping option sets the life span or TTL value of a request packet. The maximum value is 255.
* -v: This option specifies the type of service (TOS) applicable for a particular echo request.
* -r: This IPv4-based option sets the number of hops between two communicating nodes.
* -s: This option displays the time in internet timestamp format for every ping request sent/received.

Example : ```ping -c 10 -s 64 -i 1 abdfikih.site```

* -c 10: Specifies to send 10 ping packets to the destination.
* -s 64: Sets the size of the ping packet to 64 bytes.
* -i 1: Sets the interval between sending each packet to 1 second.
* abdfikih.site: The domain or host being pinged.
    
<div style="text-align:center; margin-bottom: 30px;">
  <img src="https://hackmd.io/_uploads/Sk5_T7lFT.png" alt="logo iperf" width="500" />
</div>

Explanation of Output:

1. PING abdfikih.site (76.76.21.9) 64(92) bytes of data.: Indicates that the ping command is sending 64 bytes of data to the IP address corresponding to the domain "abdfikih.site." The subsequent lines provide information about each ICMP Echo Reply received:
    * 72 bytes from 76.76.21.9: Indicates a successful response from the target IP address (76.76.21.9).
    * icmp_seq=x: Denotes the sequence number of the ICMP Echo Reply.
    * ttl=244: Represents the Time-to-Live value, indicating the maximum number of hops the packet can traverse before being discarded.
    * time=x ms: Shows the round-trip time (RTT) in milliseconds for each response.
2. The summary section at the end displays statistics for the entire ping session:
10 packets transmitted, 10 received, 0% packet loss: Indicates that all 10 packets were successfully received without any loss.
    * time 9020ms: Represents the total time taken for the entire ping session.
    * rtt min/avg/max/mdev = 26.613/40.121/58.705/10.618 ms: Provides the minimum, average, maximum, and standard deviation of the round-trip time for all responses.