---
title: "Discussion on \"使用SOCK_DGRAM类型的socket实现的ping程序\" | Hashnode"
description: "Discussion on \"使用SOCK_DGRAM类型的socket实现的ping程序\". SOCK_DGRAM类型的socket常用于UDP通信，本文将尝试把这种socket用于ICMP协议，并完成一个简单的ping程序。使用ping去测试某个主机是否可用可能是一件很平常的事，尽管ping非常普通，但是编写一个实现ping功能的程序却并不是那么简单，因为ping使用的ICMP协议并不是一个应用层协议，在网上看到的实现ping的例子大多使用raw socket去实现，不仅增加了解析IP报头的麻烦，而且还需要有root权限才能运行，本文简要介绍ICMP协议，并给出一个使用普通的常用于UD..."
url: "https://hashnode.com/posts/180020-implement-ping-program-with-sock-dgram/64140ad8ada09a4457002d52"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [使用SOCK\_DGRAM类型的socket实现的ping程序](https://whowin.cn/180020-implement-ping-program-with-sock-dgram?utm_source=hashnode&utm_medium=feed)

SOCK\_DGRAM类型的socket常用于UDP通信，本文将尝试把这种socket用于ICMP协议，并完成一个简单的ping程序。使用ping去测试某个主机是否可用可能是一件很平常的事，尽管ping非常普通，但是编写一个实现ping功能的程序却并不是那么简单，因为ping使用的ICMP协议并不是一个应用层协议，在网上看到的实现ping的例子大多使用raw socket去实现，不仅增加了解析IP报头的麻烦，而且还需要有root权限才能运行，本文简要介绍ICMP协议，并给出一个使用普通的常用于UD...

Mar 16, 2023 · 5 min read · whowin.cn  
Updated Oct 18, 2024  
Read the article

-   [#ping](https://hashnode.com/tag/ping)
-   [#networking](https://hashnode.com/tag/networking)
-   [#sockdgram](https://hashnode.com/tag/sockdgram)
-   [#icmp](https://hashnode.com/tag/icmp)

## Written by

[whowin](https://hashnode.com/@whowin)

@whowin · Beijing, China

  
[View profile](https://hashnode.com/@whowin)

一枚有30多年经验的退休程序员，主要从事嵌入式软件开发

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
