Use the C language to implement the PING program function

Source: Internet
Author: User
Tags header

Most people use the Ping command just as a simple way to see if the network connection to another system is normal. In this article, the author describes how to write a program that simulates the ping function in C language.

The ping command is a tool used to view the normal network connectivity of another host system on your network. The ping command works by sending an ICMP message to another host system on the network, and if the specified system gets the message, it transmits the message back to the sender in exactly the same way as the audible device used in the submarine sonar system.

For example, the ping localhost command on a Linux terminal will see the following results:

PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of data.
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 time=112 usec
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 time=79 usec
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=2 ttl=255 time=78 usec
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=3 ttl=255 time=82 usec
--- localhost.localdomain ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/mdev = 0.078/0.087/0.112/0.018 ms

As you can see from the results above, the ping command displays the test system host name and corresponding IP address, the ICMP message sequence number returned to the current host, the TTL lifetime and the roundtrip time RTT (in milliseconds, that is, 1 per thousand seconds). To write a mock ping command, this information has an enlightening effect.

To really understand the implementation of the ping command, you need to know the TCP/IP protocol used by the ping command.

ICMP, which is an error control mechanism provided by the gateway and the target host, enables them to report errors to the source of the message when they encounter errors. ICMP protocol is a protocol of IP layer, but because error report may also go through several subnets when sending to the source of the message, so it involves routing and so on, so the ICMP message should be sent through IP protocol. ICMP datagrams require a level two package before they can be sent: first, add ICMP headers to form ICMP packets, and then add IP headers to form an IP datagram. As shown in the following figure

IP header
ICMP header
ICMP datagram

IP header Format

Because the IP layer protocol is a point-to-point protocol, rather than an end-to-end protocol, it provides connectionless datagram services with no concept of ports, so the bind () and connect () functions are rarely used, and are used only to set IP addresses. Send data using the SendTo () function to receive data using the Recvfrom () function. The IP header format is shown below:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.