哪些计算机能收到PING Reply数据包?
时间: 2023-06-23 18:07:59 浏览: 86
任何与网络相连的计算机都可以收到PING Reply数据包。PING是一种网络诊断工具,用于测试两台计算机之间的连接是否正常。当一台计算机向另一台计算机发送PING请求时,如果第二台计算机正常工作并且与网络相连,则它会回复一个PING Reply数据包,从而证明连接正常。因此,只要计算机能够与网络相连并且具有IP地址,它就可以接收PING Reply数据包。
相关问题
Icpm ping包实现设置数据包大小的C++代码
下面是一个简单的使用 ICMP 协议发送 ping 包并设置数据包大小的 C++ 代码示例:
```c++
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cerrno>
#include <chrono>
#include <thread>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
const int PACKET_SIZE = 4096;
const int MAX_WAIT_TIME = 5;
const int MAX_NO_PACKETS = 4;
unsigned short checksum(unsigned short *buf, int len)
{
unsigned long sum = 0;
while (len > 1) {
sum += *buf++;
len -= 2;
}
if (len == 1) {
sum += *(unsigned char*)buf;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return (unsigned short)(~sum);
}
int ping(const char *ip, int packet_size)
{
int sockfd;
struct sockaddr_in addr;
struct timeval tv_begin, tv_end, tv;
struct icmphdr *icmp;
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int packetsize = packet_size;
int nsend = 0, nreceived = 0, nerrors = 0;
int i, j, len;
double rtt;
struct addrinfo hints, *res;
struct timeval timeout = {0, 1000}; // 1ms
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_RAW;
hints.ai_protocol = IPPROTO_ICMP;
if (getaddrinfo(ip, NULL, &hints, &res) != 0) {
return -1;
}
if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
return -1;
}
if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) {
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr = ((struct sockaddr_in*)res->ai_addr)->sin_addr;
cout << "PING " << ip << " (" << inet_ntoa(addr.sin_addr) << ")" << endl;
len = packetsize + sizeof(struct icmphdr);
memset(sendpacket, 0, len);
icmp = (struct icmphdr*)sendpacket;
icmp->type = ICMP_ECHO;
icmp->code = 0;
icmp->checksum = 0;
icmp->un.echo.id = getpid();
for (i = 0; i < packetsize; ++i) {
sendpacket[sizeof(struct icmphdr) + i] = 'A';
}
icmp->checksum = checksum((unsigned short*)sendpacket, len);
gettimeofday(&tv_begin, NULL);
for (i = 0; i < MAX_NO_PACKETS; ++i) {
if (sendto(sockfd, sendpacket, len, 0, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
++nerrors;
continue;
}
++nsend;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
fd_set fd_sets;
FD_ZERO(&fd_sets);
FD_SET(sockfd, &fd_sets);
int ret = select(sockfd + 1, &fd_sets, NULL, NULL, &timeout);
if (ret <= 0) {
continue;
}
if (recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, NULL, NULL) < 0) {
++nerrors;
continue;
}
++nreceived;
gettimeofday(&tv_end, NULL);
tv = tv_end;
timersub(&tv_end, &tv_begin, &tv);
rtt = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
struct iphdr *iph = (struct iphdr*)recvpacket;
icmp = (struct icmphdr*)(recvpacket + iph->ihl * 4);
if (icmp->type == ICMP_ECHOREPLY && icmp->un.echo.id == getpid()) {
printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%.2f ms\n",
packetsize, inet_ntoa(addr.sin_addr), icmp->un.echo.sequence, iph->ttl, rtt);
} else {
++nerrors;
}
}
printf("%d packets transmitted, %d received, %d%% packet loss, time %.2fms\n",
nsend, nreceived, (nerrors + nsend - nreceived) * 100 / nsend, rtt);
close(sockfd);
return nreceived;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "Usage: " << argv[0] << " <ip> [packet size]" << endl;
return 1;
}
int packet_size = PACKET_SIZE;
if (argc >= 3) {
packet_size = atoi(argv[2]);
}
return ping(argv[1], packet_size);
}
```
你可以通过在命令行中运行命令 `g++ -o ping ping.cpp` 来编译这个程序,并使用 `./ping <ip> [packet size]` 来运行它。其中,`<ip>` 是你要 ping 的 IP 地址或主机名,`[packet size]` 是可选参数,表示 ping 包的数据包大小,如果不指定,默认为 4096 字节。
计算机网络中的ping功能是啥
Ping是计算机网络中的一个基本工具,用于测试网络连接和检测目的主机是否可达。Ping的全称是Packet Internet Groper,意为数据包网际探测器。Ping工具通过发送ICMP协议的Echo Request消息,向目标主机发送数据包并等待响应,以测试网络连接质量。
当Ping工具发送一个Echo Request消息时,目标主机会返回一个Echo Reply消息。Ping会根据响应时间、响应状态等信息来判断网络连接的速度和稳定性。如果响应时间过长或者出现丢包现象,就说明网络连接质量不佳,可能会影响到网络的正常使用。
Ping工具通常用于网络故障排查和网络性能测试,可以帮助管理员快速诊断网络故障和优化网络性能。PING工具的使用非常简单,只需要在命令行中输入ping命令和目标主机的IP地址或域名即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)